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 messages from all of the validating methods to the user and highlight all of the fields with errors. It won’t save the data to the database until it is submitted again and passes validation.

  6. You can allow blank entries by specifying :allow_nil => true on your validation. That permits the field to either have a correct value or no value at all.

  7. The validates_exclusion_of method lets you make sure a value is outside of a given range.

  8. The :if parameter lets you define conditions where validation applies.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset