我试图在我的Rails 4应用程序中使用货币化(带有money-rails宝石)。
你如何允许用户只提交一个数字美元?当我输入100,我得到1美元,而不是100美元。
在我的模型中,我有:
monetize :participation_cost_pennies, with_model_currency: :participation_cost_currency我使用实例货币,所以用户选择相关的货币。我的表格列有参与成本、参与成本便士和参与成本货币。
以我的形式,我有:
<%= par.select :participation_cost_currency,
options_for_select(major_currencies(Money::Currency.table)),
label: false,
prompt: "Select your costs currency" %>
<%= par.input :participation_cost, label: false, placeholder: 'Whole numbers only', :input_html => {:style => 'width: 250px; margin-top: 20px', class: 'response-project'} %>在我看来,我有:
<%= money_without_cents_and_with_symbol @project.scope.participant.participation_cost %>用表格中的“参与成本”代替“参与成本”,我得到了一个没有美分的数字,当我输入100时,我得到了10,000美元(因此,相反的问题是,它在我输入的末尾增加了200 s。)
发布于 2015-06-10 17:17:19
似乎,您在数据库中使用了price列,并要求用户输入完全相同的输入。这两个不同的设置器/getter。尝试以下几点:
# add migration
rename_column :products, :price, :price_cents
# set monetize for this field inside the model
class Product
monetize :price_cents
end
# inside form use .price instead of .price_cents method
f.text_field :price在这种情况下,用户以美元设置价格,并且它将自动转换为美分,以将其存储在price_cents字段中。
发布于 2015-06-10 11:09:43
发布于 2015-06-10 14:28:55
假设表中有price_cents列。您可以尝试使用此代码在monitize之前将美元金额转换为美分:
monetize :price_cents
before_save :dollars_to_cents
def dollars_to_cents
#convert dollar to cents
self.price_cents = self.price_cents * 100
endhttps://stackoverflow.com/questions/30587638
复制相似问题