Money-Rails Gem and Instance Currencies

为君一笑 提交于 2019-12-08 10:42:22

问题


I am trying to use the money-rails gem in my rails 4 app.

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:

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)}" %>

When I test this, I only get the number that is the participation_cost. I don't get the currency.

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?

Thank you


回答1:


Steps:

  1. Add column type money

    Rails 3

    add_money :table_name, :participation_cost

    Rails 4
    add_monetize :table_name, :participation_cost

    This will give you two columns

    :participation_cost_pennies #pennies if GBP, cents if USD

    :participation_cost_currency

  2. 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

  3. Just update your fields in form and save currency as you were saving but in participation_cost_currency column.

If you don't want pennies at end or participation_cost in currency column you can modify that in Money.rb. See here



来源:https://stackoverflow.com/questions/30010565/money-rails-gem-and-instance-currencies

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!