I follow some tips to validates the numericality of a field only if presence, with:
validates_numericality_of :year, only_integer: true, allow_nil: true
or
validates_numericality_of :year, only_integer: true, allow_blank: true
But I can create it with a year like 'asdfasdf'
and rails stores a blank year.
What can be wrong?
Instead of allow_nil: true
, use allow_blank: true
.
You should also replace text_field
by number_field
in your view. With number_field
, if your characters are not only digits, it's evaluated as nil.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With