Here is what I use in my model:
before_validation :strip_dollar_sign
validates :amount_due,
:format => { :with => /^\d+??(?:\.\d{0,2})?$/ },
:numericality => {:greater_than => 0}
private
def strip_dollar_sign
self.amount_due = self.amount_due.to_s.tr!('$,','').to_f
end
If I run the line from the strip_dollar_sign function manually in the Rails Console, I get exactly what I want (i.e. $ 400 ends as 400.0), but when I use the actual form in my application, the value always ends as 0.0. Anyone get what I'm doing wrong?
source
share