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
:messagenamed 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_ofmethod lets you test against regular expressions, or you can write your own more complicated tests by extending validation through thevalidatemethod.Rails will report all of the messages from ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access