Money-Rails Gem and currency instances

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

I have a participant model in which there are attributes for each of the currencies and participation. My goal is for users to specify the currency for each instance and the amount of the cost of participation.

In my member model, I:

monetize: participation_cost, with_model_currency :: amount_currency

In my form, I ask users to select a currency and indicate the 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 opinion, I want to display the currency and amount. I currently have a line:

  <%= "#{@project.scope.try(:participant).try(:participation_cost)} #{@project.scope.try(:participant).try(:currency)}" %> 

When I check this, I get only a number that is a member of the participation. I do not receive currency.

In my money.rb initializer, I have:

  config.default_currency = :gbp 

Can anyone help? I do not know how to use this stone. I followed the user manual, but it gets to how to configure the model to select the currency based on the instance. Has anyone successfully used it for this purpose?

thanks

0
source share
1 answer

Steps:

  • Add Type Column Size

    Rails 3

    add_money :table_name, :participation_cost

    Rails 4
    add_monetize :table_name, :participation_cost

    This will give you two columns

    : membership_cost_pennies # pennies if GBP, cents if USD

    : participation_cost_currency

  • In the model, the bottom row overrides the default and global currency for this column and will use the currency in this column

    monetize :participation_cost_pennies, with_model_currency: :participation_cost_currency

  • Just update your fields on the form and save the currency at the time of saving, but in the employee_cost_currency column.

If you do not want a penny at the end or a member_cost in the currency column, you can change this in Money.rb. See here

+5
source

Source: https://habr.com/ru/post/986900/


All Articles