Posted about 1 year ago. Visible to the public.

Validations [1d]

Basic validations

Read the Rails Guide on ActiveRecord Validations Archive . You should have an overview which kinds of validations are built into Rails.

Also read Testing ActiveRecord validations with RSpec.

Make the following changes to your MovieDB:

  • Make sure your MovieDB models have validations for all required fields.
  • Make sure you have specs for all your validations in MovieDB.
  • Use Shoulda matchers where you think they improve the readability your code.

Working with error messages

Understand how to use the #errors method Archive to inspect the reasons why a record is invalid.

Make the following changes to your MovieDB:

  • There is no longer a full list of error messages at the top of each failed form.

  • When a form could not be saved, only the fields with errors show their respective error messages.

    Your forms should be triples of label, input and error message:

    Title [_____________________] Title must not be blank Year [____-5] Year must be a positive number
  • If a field has more than one error, only show the first error.

  • A web app has a lot of fields, and each must be able to display an error message. Make it easy for a form view to display the error message for a given record and field. Remove as much code from the view as possible.

  • Style your form so that when a field is invalid, the label and error message is colored red. In addition, an invalid field control should have a red border.


    Rails adds an extra element around invalid inputs to help with styling. Inspect an invalid field in your browser to see how it works.

  • Add support for a positive and negative flash message Archive in your application layout. When a form could be saved successfully, show a flash like "Movie saved". When a form is re-rendered due to a validation error, show a flash like "Could not save".


    Unlike @variables set by a controller, flash messages persist to the next request. This is to support the case of setting a flash and then redirect.

    If you have trouble with flash messages appearing longer than they should, you can clear flash messages after displaying them using flash.clear.

Custom validations

Add the following feature to your MovieBD:

  • Movies have a release year. This is a number. It can also be missing (nil).
  • Movies have a film type. This is a string that is either "bw" (black and white) or "color".
  • The default film type is "color".
  • In the movie form we can switch between black and white and color movies using a <select>.
  • Add a validation that ensures that year > 1895 for black and white movies and year > 1917 for color movies.

Add tests for your validation. Make sure you have a test for each case.

Your development team has a full backlog of feature requests, chores and refactoring coupled with deadlines? We are familiar with that. With our "DevOps as a Service" offering, we support developer teams with infrastructure and operations expertise.

Owner of this card:

Henning Koch
Last edit:
2 months ago
by Fabian Schwarz
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Henning Koch to makandra Curriculum
This website uses short-lived cookies to improve usability.
Accept or learn more