Money-Rails Gem with Rails 4 - Instance Currencies
Asked Answered
C

3

4

Im trying to use monetise in my Rails 4 app (with money-rails gem).

How do you allow a user to submit a number dollars only? When I input 100 I get $1 instead of $100.

In my model, I have:

monetize :participation_cost_pennies, with_model_currency: :participation_cost_currency

I am using instance currencies, so users select the relevant currency. My table has columns for participation cost, participation cost pennies and participation cost currency.

In my form, I have:

   <%= 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'} %>

In my view, I have:

   <%= money_without_cents_and_with_symbol @project.scope.participant.participation_cost  %>

By replacing 'participation cost pennies' with participation cost in the form, I get the number to show as a whole number without cents I now get $10,000 when i enter 100 (so the reverse problem in that it is adding 2 00s to the end of my input.

Castanon answered 2/6, 2015 at 4:56 Comment(2)
This sounds like a strong-params issue. Have you white-listed your params correctly in the controller?Zoi
Hi Sir Bertly, I have white listed the params for the participant model in each of the participants and projects controllers. I haven't added them to my scopes controller. I don't think I need them there because scope is nested within projects. Question updated with white listed params.Castanon
S
3

Seems, you use price column inside the database and ask users to input exactly same input. This two different setters/getters. Try the following:

# 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

In this case user set price in dollars and it will be automatically converted to the cents to store it inside price_cents field.

Schoolbag answered 10/6, 2015 at 17:17 Comment(0)
A
1

In the money-rails gem, prices are saved in cents, so a price of 10 will be saved as 1000 price_cents. This is made on purpose to avoid floating point rounding errors.

To display the right price(without the cents), just call the price method on your object and it will display the correct non-cents price

Amplexicaul answered 10/6, 2015 at 11:9 Comment(3)
How do I write the helper function?Castanon
Multiply the input by 10.Amplexicaul
For those who find themselves here, do not convert the input by yourself, the gem already does that for you, just populate the price field and have monetize :price_cents on your modelWhine
B
1

Assuming you have price_cents column in your table. You can try this code to convert dollar amount to cents before monitize:

monetize :price_cents
before_save :dollars_to_cents

def dollars_to_cents
    #convert dollar to cents
    self.price_cents = self.price_cents * 100 
end
Basrhin answered 10/6, 2015 at 14:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.