Test Your Knowledge

Quiz

  1. How much type-checking does Rails do against the types you specified in your migrations?

  2. What happens when a validation error is reported?

  3. How do you customize the error notifications users see when their data doesn’t match up to your validator’s expectations?

  4. How do you test the detailed syntax of user-entered data to make sure it matches a particular pattern?

  5. If there’s more than one error reported by the validator methods, what does Rails do?

  6. How do you specify if something may be either valid or blank?

  7. How do you specify that a value has to be outside of a particular range?

  8. How can you specify that a validation applies only if another value in the form has a particular value?

Answers

  1. 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.

  2. 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.

  3. 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.

  4. The validates_format_of method lets you test against regular expressions, or you can write your own more complicated tests by extending validation through the validate method.

  5. 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.