How to show errors to the user when using forms in Rails.
By: Javi Huerta Jr
Tags: Ruby, Ruby on Rails, Flatiron School, Forms

STEP ONE:

Okay! So let’s add validations to our model the user will be able to create or modify. Head over to app/models and open the .rb file of the model we will be adding validations to.

class Dog < ApplicationRecord

validates :name, presence: true

validates :age, presence: true

end

These validations confirm the dog instance that the user can create or edit will have a name and age for the dog before it is saved into the database.

STEP TWO:

Awesome! Validations added! Now we need to add some embedded ruby to our view file file that contains the form. Find this file in: app/views/dog/new.html.erb

Your current View file should look something like this:

<h1>CREATE NEW PUP!</h1> <%= form_for @dog do |f| %>

<%= f.label :NAME %>

<%= f.text_field :name %>

<%= f.label :AGE%>

<%= f.text_field :age%>



</br> <%= f.submit “SUBMIT” %> <% end %>

Now we need to add our embedded ruby code, this will show the user any errors that caused the form to not save successfully. The code needs to be above the form code. Code preferably should be under the header, so that the user can clearly see the errors.

<% if @dog.errors.any? %>

<% @dog.errors.full_messages.each do |message|%>\

<h3><%= message %> </h3>

<%end%>

<%end%> </br>

The code shown above is an if else statement. This statement is pretty much just saying if there are any errors in the model instance as to why it didn't save, show them to the user. Which brings up an interesting topic, we will discuss in our next and final step. Your view page with the form should look something like this afterwords:

<h1>CREATE NEW PUP!</h1> <% if @dog.errors.any? %>

<% @dog.errors.full_messages.each do |message|%>\

<h3><%= message %> </h3>

<%end%>

<%end%> </br> <%= form_for @dog do |f| %>

<%= f.label :NAME %>

<%= f.text_field :name %>

<%= f.label :AGE%>

<%= f.text_field :age%>



</br> <%= f.submit “SUBMIT” %> <% end %>

STEP THREE:

You now have everything you need to show errors to the user! Now we just need to confirm the create method in our model controller was made correctly. We need to make sure if the validation fails, that the form page will be reloaded to the user. How do we do this? An If Else statement of course! Your create method needs to look something like this:

def create

@dog = Dog.new(users_params)

if @dog.save

redirect_to dog_path(@dog)

else

render :new

end

end

With this code, we use the .save method which is important in using the validations we created for the Dog model. The save method is a ruby on rails method that checks if the validations pass as true and if they do return true the save method will save them to the database.

We also use the render :new, which is also important for showing the error messages. We could use redirect_to new_dog_path, but think about it… if it goes to the new form page, how will it return an error message without the instance variable? Because of this we use render :new, this method will render the form again in the page holding onto the dog instance variable. This allows us to show the user any errors preventing the dog from being saved to the database.