2

I have money-rails gem installed and it's working flawlessly. However, there are no validations on money objects when I create a new model record. If I try to input letters into the money field, the form submission is successful, it just sets the value to 0. How do I get those validations working? I have no code for the actual validation, seeing as money-rails on github states that it has validations included, but I have tried validates_numericality_of to no avail.

EDIT Yes I have read the docs extensively, and tried the validation option suggested on Github.

zenben1126
  • 685
  • 1
  • 7
  • 25

1 Answers1

3

I have this example hope it could help you

monetize  :price_cents, :numericality => {:greater_than => 0, :less_than => 10}

validates :price, :format => { :with => /\A\d+(?:\.\d{0,2})?\z/ }

just like this one of them (monetize validation) will validate the numericality stuff and the other one will validate the format.

  • your second line works, but I do have a side-note: after giving an error and not persisting to the db, in an edit action, the render 'edit' renders the form with a new rounded value. I'd like it beter if say 12,5056, re-renders to 12,50 rather than 12,51. – Code-MonKy Sep 25 '16 at 19:09
  • Remember to add the column on DB with the '_cents' suffix instead 'price' will be 'price_cents' and add attr_accessor for price. `attr_accessor :price` About the rounded values, you should work in a method or check the documentation, not sure if there is something for it. – jaime gonzalez Sep 28 '16 at 18:09