Money-Rails Gem and Instance Currencies
Asked Answered
S

1

0

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

Sebastiansebastiano answered 3/5, 2015 at 5:31 Comment(1)
Can you also share your model code?Internal
O
5

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

Outlast answered 7/5, 2015 at 15:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.