How to Use Rails Validation Helpers

Ariba Khan
The Startup
Published in
5 min readSep 28, 2020

--

In this blog I wanted to focus on using validations in rails. What exactly are validations and why should we as programmers incorporate them in our rails applications? Validations are a special kind of method that we can use to protect our database from information we don’t want to save in it. For example, let’s say our database accesses your bank account information. We would want to be able to input our routing number for the account. We can ensure that through validations that only numbers can be input in that space. Therefore, if someone is trying to input his or her email address in the same spot where the routing numbers are supposed to go then the request will not go through.

Why is it important to validate? Through validations we can ensure that users are using the app in the correct way and avoid any type of scam or fraudulent data. Following the example above if a person were to type joeshmoe123@gmail.com in the spot where we would be expecting our bank routing number we can assume that this information is clearly not the persons bank routing number. This type of data is considered invalid data and is exactly the type of data we want to avoid!

What are the different kinds of information that we can validate? Well, there are many different validation helpers we can use. From name uniqueness, specific number ranges, and length there are many to choose from. To find a list of common helper methods I would highly recommend using the Ruby Rails guides because they are extremely easy to use and follow. Here is the link for reference :https://guides.rubyonrails.org/active_record_validations.htm. l .

Now for our example let’s say we are creating a project with campers. We want to ensure that each campers name was unique so we’re not repeating any names. When we go into that document we can see there is a long list of helpers we can use; scrolling through what validation helper can we use to confirm each campers name is unique. Could it possibly be uniqueness? YES! We find uniqueness so we click it.

List of Validation Helpers!

After clicking uniqueness it takes us to a page where it shows us the below example.

The example tells how to incorporate the validation helper it into our code. Using a validation helper; these are three steps I followed in order for the validation to work.

  1. In our Camper model right underneath our associations we would type in:

validates :name, uniqueness: true

This is exactly how the Ruby on rails documents shows us how to to type. Make sure to write it before the end of the class. Also make sure the syntax exactly matches the rails guides example. I would always forget the the “,” after name!

2. Next step is to go into our campers_controllers.rb file. We are going to want to update our create method. If you would like a detailed reasoning as to why we are going into our create method please reference this(https://learn.co/tracks/web-development-immersive-2-0-module-two/rails/validations-and-forms/activerecord-validations). It states, “The only way to trigger validation without touching the database is to call the #valid? method.” So this is why were write out an if else statement with if valid? We want to be able to ensure that if we are ensuring the campers name is valid then it gets saved and created. But if the campers name is not unique we want to be able to give the user redirection and let them know! We are going to utilize flash.

Next step would be to go into our views → campers folder → and in the new.html.erb we would want to update it so that when we go actually hit the flash method in our create method, and let’s say the name is not unique we can let the user know what exactly the error was.

Below is an example of what the exact syntax would look like. Take note of a few important details. Notice how in my create method above I typed “my_errors” in the brackets. This is very important to distinguish from the normal “error” in the pipes in the new file. DO NOT confuse the two errors- they are referring to two separate errors. It is very easy to just use plain error and then not have the code work properly because it is unable to tell the difference. So do yourself a favor and just clearly name however is easiest for you. My instructor taught us my_errors so that is why I used that!

Another important detail to take note of is the style = “color:red;” this is NOT necessary but it is another cool feature my instructor taught us that is super easy to include. Also since errors are normally red its a nice visual to add that also makes sense.

Now if we wanted to test to make sure our validation works- we can type in a campers name that is already in our database- so therefore not unique. We would hopefully want to get back errors that say we cannot use that camper name! I’ll use a camper name that I know is already in use and see if I can create it. I know Rose is camper name that has already been taken so I’ll use that.

There you have it- name has already been taken! Yay! Never been so happy to get an error! Hopefully this cleared any confusion on how to use validation helpers! Also you make notice there is a second error that states “Age must be greater than 7”; this is because I added another validation that made the age of the camper be between 7–18 years old. I had typed in 3 for the age so therefore it gave be back that response. Below is the validation helper I used for that!

Hopefully this article helped clear up any confusion on how to validation helpers. Thanks for reading :)

--

--