A float is a number, and regular expressions are for strings.
It looks like when you enter a string for float, it will automatically convert to 0.0 automatically using Rails.
Do you have a default value (0.0) in a column? If so, then you can try to remove it and use only validates_presence_of :price .
Something to try: instead of putting the row directly in the price column, put it in price_string attr and use the before_save to try to convert the row to price. Something like that:
attr_accessor :price_string before_save :convert_price_string protected def convert_price_string if price_string begin self.price = Kernel.Float(price_string) rescue ArgumentError, TypeError errors.add(ActiveRecord::Errors.default_error_messages[:not_a_number]) end end
And in your form, change the name of the text field to :price_string .
source share