Skip to content

Getting Started with Rails 7 Episode 8: Model Validations and Form Error Messages

This is the 8th video in the Getting Started with Rails 7 series. In this video Mike demonstrates that you can currently create an Article record with an empty title and body. In order to solve the problem, he uses Rails model validations to ensure the data for the Article record is in good order before trying to save to the database. He first creates failing tests in the ArticleTest model test class and then adds in the validations to make them pass. After that, he adds embedded Ruby to the new view template to display the error messages in the view if any exist.

This video covers:

  • 00:00:10 Introduction and demonstrate lack of validations
  • 00:02:55 Write failing model tests in ArticleTest class, both happy and unhappy paths
  • 00:09:13 Add validations to the article model incrementally to make tests pass
  • 00:11:27 Review the validation error message view code in the guide
  • 00:17:30 Add test for bad parameters to ArticlesControllerTest and troubleshoot assert_select unexpected behavior. Solve by adding classes to the error message divs.
  • 00:24:10 Add back in the error messages to the view to make the tests pass and explore in the application UI
  • 00:25:05 Review the rest of this section of the guide. Demonstrate preservation of bad form input data if invalid
  • 00:25:50 Add link_to helper in articles index view to new article form
  • 00:29:11 Review, commit, and push the code

The code for this series is open source and available on GitHub. View the commit for this video here.

Leave a Reply