How to Use Rails Validation Helpers

Ariba Khan
Sep 28 · 5 min read
Image for post
Image for post

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.

Image for post
Image for post
List of Validation Helpers!

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

Image for post
Image for post

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:
Image for post
Image for post

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.

Image for post
Image for post

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!

Image for post
Image for post

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.

Image for post
Image for post

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!

Image for post
Image for post

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

The Startup

Medium's largest active publication, followed by +734K people. Follow to join our community.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store