Checking math equation with checking rails

I want the registration form on my site to have a field that takes the sum of a math equation and uses validation to validate it. What's the best way to do this?

i.e

What is 6 + 9? [8]

Error message: you entered the wrong number

0


a source to share


1 answer


Override the method validate

in your model class. Remember that the model object created for the action new

is a different instance than the one created for the action create

, so you need to store the random seed or math expression somewhere in your form so you can recreate it during validation.

Then something line by line:



def validate
  unless math_equation_answered?
    errors.add("math_answer", "is incorrect")
  end
end

      

The implementation is math_equation_answered?

up to you and math_answer

should be changed in any field in the model that you use to respond to the user.

+1


a source







All Articles