From the course: Ruby on Rails 5 Essential Training
Unlock the full course today
Join today to access over 22,700 courses taught by industry experts or purchase this course individually.
Form errors - Ruby on Rails Tutorial
From the course: Ruby on Rails 5 Essential Training
Form errors
- In this movie we'll learn how to handle and display form errors. When we're talking about form errors, what we're really talking about are reasons why an object can't be saved to the database. We aren't going to talk about data validation until the next chapter but we can get started by using a very simple validation, which is validates_presence_of :name. We'll add that to our subject model. That will require that the subject has some value for the name attribute or else Rails won't allow it to be saved to the database. Not only will the save or update fail but Rails will add an error to object.errors which is an array-like structure for holding any validation errors. Remember, in our controller after a save or update fails, we're going to re-render the form to give the user a chance to fix any problems with the form and try submitting again. We'll also have access to object.errors and can use it to display information about exactly which problems need fixing. Without the display of…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.