0

In my Ruby on Rails app, I've got a model called Post which has a column named "Price" in it's database table; this column is decimal type with a precision of 8 and scale of 2.

The form for a new Post has a 'text_field' for :price, and in the Post show view, I've setup "Price" to display this way ->

<p>
  <strong>Price:</strong>
  <%= number_to_currency(@post.price) %>
</p>

-My problem: the Price only displays correctly if the user inputs the value WITHOUT any commas. If the user puts in their desired numerical value with commas included, the Price ends up rendering as if they meant to put a decimal; hence making the displayed value too low

(example: If a user puts 67,000 as the value for the Price field, it will be saved/displayed as 67.00).

How could this be fixed so that even if the user includes commas in their originally typed value for Price, it will be displayed correctly...? And inversely, if the user doesn't include commas, a method so that the resulting output to the Show view is correctly styled with the commas for thousands/millions places...?

Shawn
  • 47,241
  • 3
  • 26
  • 60
hworth
  • 303
  • 3
  • 17
  • Have you tried setting `step` attribute to the input, like here?: http://stackoverflow.com/a/19012837/2399861 – Ruby Racer Apr 30 '15 at 21:59
  • How would I do that to this line: <%= f.text_field :price, class: "form-control" %> ? – hworth Apr 30 '15 at 22:12
  • 1
    I believe this: `<%= f.number_field :price, nil, step: 'any', class: "form-control' %>`, from here (not tested thought): http://apidock.com/rails/ActionView/Helpers/FormTagHelper/number_field_tag – Ruby Racer Apr 30 '15 at 22:45
  • It gives me a `wrong number of arguments (3 for 1..2)` error when I replace the line with your suggested one... any idea what that indicates? – hworth Apr 30 '15 at 22:48
  • By removing 'nil' from the code, it made the rest of the attributes work! Which is exactly what I was looking for, thanks for the answer. – hworth May 01 '15 at 03:05

2 Answers2

1

Big thanks to users "Ruby Racer" and "Mark H" for leading me down the right path to the solution!

Here is the corrected version of the form for a new Post:

<%= f.label :price %>
<%= f.number_field :price, step: 'any', class: "form-control" %>

This allows the Price to be correctly displayed with two decimal places, and simply won't accept commas from the user as initial input.

hworth
  • 303
  • 3
  • 17
0

If commas in the user's input are causing problems, remove them.

<p>
  <strong>Price:</strong>
  <%= number_to_currency(@post.price.remove(",")) %>
</p>
# http://api.rubyonrails.org/classes/String.html#method-i-remove

For display, the formatting is controlled by the locale argument of number_to_currency(). http://api.rubyonrails.org/classes/ActionView/Helpers/NumberHelper.html#method-i-number_to_currency

If your application will be used by users who actually do use the comma as a decimal separator, you'll want to handle that situation, too.

Mark H
  • 585
  • 3
  • 11
  • I just tried your .remove(",") trick with number_to_currency as you wrote it, but I'm receiving an **undefined method `remove' for #** error, any ideas what that means? The line in question raising the error is '<%= number_to_currency(@post.price.remove(",")) %>' – hworth Apr 30 '15 at 21:41
  • It means that the variable held by `@post.price` is a BigDecimal. I had assumed it was a string. Your problem occurs when the user's input is stored in the `@post`, not when it is displayed. That code converts the text to a BigDecimal. Edit your question and add the code that takes user input and puts it in `@post.price`. – Mark H May 01 '15 at 02:37