user1502223 user1502223 - 7 months ago 34
Ruby Question

Rails remove percentage symbol for numeric validation

I'm trying to validate a number but via jquery there is a % sign added to the interface

=> '10.4%'


So when i try to validate numericality it says that it is not a number

is there a rails way to validate this?

I've tried doing this in my model

before_validation :convert_number

private

def convert_number
self.number.to_f
end


but it is still throwing the validation error that it is not a number



Answer

Probably, Issue is type of number is not decimal.

 before_validation :convert_number

 private

 def convert_number
   self.number = number.to_d
 end