Reputation: 13
I am having some trouble with custom validation in rails. I want to validate that only numbers are being put into the field where they choose dial in the drop down list.
my code thus far
validate :validate_numericality_of_dial_arg
def validate_numericality_of_dial_arg
if action_type == "dial"
return action_dst =~ /^\d+$/
else
errors.add(:base, "must be numbers")
end
end
right now I am getting the error to pop up saying must be numbers even if the user enters numbers
Upvotes: 1
Views: 325
Reputation: 6455
If you don't want to insist on using a custom validator, you can always use some rails magic:
## app/model.rb
validates :action_dst, numericality: true, if: -> {action_type == "dial" }
Upvotes: 5