Less Hating When You’re Validating!

Stephanie Segura
4 min readFeb 28, 2021

--

Written by Stephanie Segura | February 28, 2021

Thank you for visiting this post! Be sure to also take a look at my other work on LinkedIn, Github, and my website.

Oh, nooo! Mr. Bananas is about to lose it!

Hello, and welcome to the blog! If you’re here, it’s safe to assume that you, my friend, are using Ruby on Rails. Hooray! However exciting it may be, creating a full functioning web application can be seriously daunting, especially when we need to incorporate validations!

What are Validations and how can they be useful to you? Validations are used to ensure that only valid data is saved into your database. Rails provides built-in helpers for some realistic/common needs a dev might need and you can even create your own custom validations too!

Frustrated and don’t know where to begin implementing your validations?

No need to fear, Le Stephanie is here! Let’s get after it!

Let’s say that you’re working with a One-to-Many Relationship where an Instructor has many students, and a Student belongs to an Instructor.

Association Model

You’ve created the Index and Show aspects of your MVC model, and now you’re wanting to Create new instances of the Student. Awesome! Before setting up our create method in the Student Controller, we must first determine what validations we’ll be passing in and implement them in the Student Model.

Let’s say our student has a name and age, and we want to make sure that each student’s name is unique and that they are over the age of 18. We would write it in the Student Model like so:

Let’s break down our code above.

We’re passing in a couple validation arguments for name and age; Let’s start by going over the name validation. By using “uniqueness: true” we are setting up the validation to throw an error if a student tries to enroll under a duplicate name. By using “presence: true”, we’re ensuring that a student doesn’t try to enroll without having the name section filled out.

Moving on to the age validation; by using “numericality: { greater_than: 18 }”, we’re setting this up to throw an error if a student tries to enroll under the age of 18.

Now that we’ve set up our validation foundation in the Model, it’s time to move on to the next step! Using “.valid?” and flash errors, we can make our Create method in our Student Controller.

Create Method for the Student Controller

We just typed lots of code! What does this all mean? Let’s get into it.

We’re creating a new instance of a student and assigning it to the new_student variable. Next we’re setting some conditionals with an if and else statement. We’re saying, If the new_student is NOT valid, or fulfilling the validation arguments we declared in the Student Model, flash an error for the particular new student instance we created. Else, if the student meets all the required validations we established, save the student instance and redirect them to their student show page(side note: make sure to check your routes!)

We’ve established our validations in our Student Model and Controller, or our M and C from the MVC model, now we need to bring it home on the front end, or View side. In order to create a new student, we need to establish a form in the Student View folder. You can do this two way: By making the form in your create.html.erb file or creating a partial form that can be rendered in multiple places within the Student View folder. I personally prefer the partial form option, since it allows me to write less code!

Let’s create our partial form in _form.html.erb:

Great! Now that our form is created, we could just render the form in our new.html.erb file, but how can we tell our form that we want to pass in flash errors for validation conditions? That’s a great question, my friend. Let’s find out!

Let’s break down the code above! Not only are we rending the form in the new.html.erb file, we are also establishing the flash errors to “flash an error” if the validations we established in the model aren’t met.

Your errors should look something like this:

Example of Flash Errors on Form Submission
This Wizard is You! You is Wizard!

What is this sorcery!? Super cool! There are so many different kind of validation arguments you can pass into the Model!

You can reference more information on this at: https://guides.rubyonrails.org/active_record_validations.html

Thank you so much for stopping by! I hope this gave you a better understanding of how to use validations in ruby on rails.

Best of luck to you all and Happy Coding!

--

--