Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 22nd, 2012  |  syntax: None  |  size: 0.47 KB  |  hits: 17  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Rails : Validates_format_of for float not working
  2. validates :price, :format => { :with => /^[0-9]{1,5}((.[0-9]{1,5})?)$/, :message => "should be float" }
  3.        
  4. attr_accessor :price_string
  5.  
  6. before_save :convert_price_string
  7.  
  8. protected
  9.   def convert_price_string
  10.     if price_string
  11.       begin
  12.         self.price = Kernel.Float(price_string)
  13.       rescue ArgumentError, TypeError
  14.         errors.add(ActiveRecord::Errors.default_error_messages[:not_a_number])
  15.       end
  16.     end