Money-Rails Gem with Rails 4 - Instances

I am trying to use monetization in my Rails 4 application (with gems with money rails).

How do you allow a user to send only dollars? When I enter 100, I get $ 1 instead of $ 100.

In my model, I have:

monetize :participation_cost_pennies, with_model_currency: :participation_cost_currency 

I use instance currency, so users select the appropriate currency. My table has columns for participation costs, quotes for participation costs and participation costs.

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 opinion, I have:

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

Replacing the "participation cost quotes" with the participation cost in the form, I get the number for display as an integer without cents. Now I get $ 10,000 when I enter 100 (so the inverse problem is that it adds 2 00s to the end of my input.

+4
ruby-on-rails money-rails
source share
3 answers

It seems you are using the price column inside the database and ask users to enter exactly the same input. These are 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, the user will set the price in dollars, and he will be automatically converted to cents to save it inside the price_cents field.

+2
source share

In the money-rails gem, prices are kept in cents , so it’s normal that 100 exits are $ 1, since that’s really 100 cents. $ 100 will be 10,000. This is done on purpose, to avoid floating point rounding errors .

You can write a helper function to convert your input to cents if you want to deal with dollars in your application.

+1
source share

Suppose the table has a column called price_cents . You can try this code to convert the amount in dollars to cents before you plan:

 monetize :price_cents before_save :dollars_to_cents def dollars_to_cents #convert dollar to cents self.price_cents = self.price_cents * 100 end 
+1
source share

All Articles