I am new to rails and am making a simple form. In the form I have a field that asks “Do you like ice cream?”
<%= f.label "Do you like ice cream?" %>
<%= f.select :support_type, [['Yes'],['No']] %>
<%= f.submit "Submit" %>
I would like to validate that the user chose “Yes” and spit out an error and highlight the form (like validates_presence_of would do for an incomplete form) if the user submits the “No” answer. I created a validate method:
def validate
if self.support_type == 'Yes'
errors.add(:support_type, "You are crazy")
return false
end
end
but this didn’t seem to work for me. Any help or hints would be much appreciated. Thanks.
in your model do the following:
Validate is a class method which needs to be given a validator method name.
You can have several of them: