I am new at Ruby on Rails.
I was trying to validate format of one of the attribute to enter only float.
validates :price, :format => { :with => /^[0-9]{1,5}((\.[0-9]{1,5})?)$/, :message => "should be float" }
but when I enter only character in price, it accepts it and show 0.0 value for price.
can anybody tell, what is wrong in this or why this happens?
A float is a number and regular expressions are for strings.
It appears that when you enter a string for the float, it gets converted as 0.0 automatically by Rails.
Do you have a default (0.0) on the column? If yes, then you may try removing it and use
validates_presence_of :priceonly.Something to try: instead of putting the string directly into the
pricecolumn, put it into aprice_stringattr and use abefore_savecallback to try to convert the string to price. Something like that:And in your form, change the name of the text_field to
:price_string.