Test Your Knowledge
Quiz
How much type-checking does Rails do against the types you specified in your migrations?
What happens when a validation error is reported?
How do you customize the error notifications users see when their data doesnât match up to your validatorâs expectations?
How do you test the detailed syntax of user-entered data to make sure it matches a particular pattern?
If thereâs more than one error reported by the validator methods, what does Rails do?
How do you specify if something may be either valid or blank?
How do you specify that a value has to be outside of a particular range?
How can you specify that a validation applies only if another value in the form has a particular value?
Answers
Rails does no type-checking by default. It just coerces the data that came in to the matching type, and if it doesnât match, too bad. You have to provide explicit validation code for every field you create.
Validation errors block the saving of records. The model sends the data back through the controller to the view, adding messages about what is wrong with the data so the view can display them.
The
:message
named parameter lets you provide a specific notification. Rails will do some notifying by default, in basic cases, but youâre generally wise to add your own messages.The
validates_format_of
method lets you test against regular expressions, or you can write your own more complicated tests by extending validation through thevalidate
method.Rails will report all of the ...
Get Learning Rails: Live Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.