Money-Rails宝石和实例货币 [英] Money-Rails Gem and Instance Currencies
问题描述
我正在尝试在我的Rails 4应用程序中使用money-rails gem.
I am trying to use the money-rails gem in my rails 4 app.
我有一个参与者模型,该模型中的每种货币和participation_cost具有属性.我的目标是让用户为每个实例指定一种货币以及参与费用的数额.
I have a participant model which has attributes for each of currency and participation_cost in it. My objective is for users to specify a currency for each instance, and an amount of the cost of participation.
在我的参与者模型中,我有:
In my participant model, I have:
通过:model_currency::amount_currency货币化:participation_cost
monetize :participation_cost, with_model_currency: :amount_currency
在我的表单中,我要求用户选择一种货币并指定以下金额:
In my form, i ask users to select a currency and specify an amount as follows:
<%= f.input :participation_cost, label: 'What amount will you pay for participation costs?', :label_html => { :class => 'question-participants' }, placeholder: 'Whole numbers only', :input_html => {:style=> 'width: 250px; margin-top: 20px', class: 'response-participants'} %>
<br><br>
<%= f.input :currency, label: 'Select your costs currency', label_html: {class: 'fundingspace'}, collection: ["AUD Australian Dollars", "GBP British Pounds", "USD US Dollars" ], prompt: "Choose one" %> </div> <br>
在我看来,我想显示货币和金额.我目前有一个字符串,如下所示:
In my view, I want to display the currency and the amount. I currently have a string as follows:
<%= "#{@project.scope.try(:participant).try(:participation_cost)} #{@project.scope.try(:participant).try(:currency)}" %>
当我对此进行测试时,我只会得到参与费用(cost-in-cost)这个数字.我没有货币.
When I test this, I only get the number that is the participation_cost. I don't get the currency.
在我的money.rb初始化程序中,我有:
In my money.rb initialiser, I have:
config.default_currency = :gbp
任何人都可以帮忙吗?我不知道如何使用这个宝石.我遵循了用户指南,但仅能为基于实例的货币选择建立模型.有没有人成功地将其用于此目的?
Can anyone help? I don't know how to use this gem. I've followed the user guide but it only gets as far as setting up the model for instance based currency selections. Has anyone successfully used it for this purpose?
谢谢
推荐答案
步骤:
-
添加列类型money
Add column type money
路轨3
add_money :table_name, :participation_cost
路轨4
add_monetize :table_name, :participation_cost
Rails 4
add_monetize :table_name, :participation_cost
这将为您提供两列
:participation_cost_pennies #英镑(如果是GBP),美分,如果是美元
:participation_cost_pennies #pennies if GBP, cents if USD
:participation_cost_currency
在模型中 下面的行覆盖了此列的默认货币和全球货币,并将使用此列中的货币
In model The below line override default and global currency for this column and will use currency in this column
monetize :participation_cost_pennies, with_model_currency: :participation_cost_currency
只需以表格的形式更新字段并保存您所保存的币种即可,但请保存在"participation_cost_currency"列中.
Just update your fields in form and save currency as you were saving but in participation_cost_currency column.
如果您不想在货币列中结尾处输入几分钱或一分钱的费用,则可以在Money.rb中进行修改.请参见此处
If you don't want pennies at end or participation_cost in currency column you can modify that in Money.rb. See here
这篇关于Money-Rails宝石和实例货币的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!