Rails 101 | validates_currency?
I've searched high and low, but I could not find a solution, to what I think seems like a very common task.
I开发者_开发知识库n a form I want to have a text input that accepts currency strings (i.e. $1,000,000 or 1000 or $12.12 and in an ideal world even 1 million)
In the database I want to keep the value as a integer.
What is the best way to (a) validate that the input is a valid currency string, (b) when displaying the form, to display it formatted.
For date, i found the a great gem: validates_timeliness, which allows :
validates_date :my_date, :allow_blank => true
I would love to find a similar syntax for currency.
Thanks!
Check out rails_money
. I think it does everything you need, including handling $ in the input fields and storing the value in cents. In fact, you just create your column as price_in_cents
, and it does the rest.
精彩评论