About forms and validations in ruby on rails

Validations in ruby ​​on rails help us to filter information from a form in order to store it in our database. By correctly validating and setting up our forms, we can ensure that only relevant data is going to be saved.

An example of this is when you are going to fill out an online form and the page does not allow you to submit the questionnaire until you correctly fill out certain fields, such as not leaving empty spaces, using numbers instead of letters, etc. Imagine that you have created an online casino application and you want to ensure that all underage users cannot have access to it.

In this case we can create a field where the user has to enter their date of birth. With this we can start to work on the following filters.

1. Prevent incomplete information

You can create a predefined active records validation presence that prevents the user from continuing in the program until after fully completing the form.

validates :title, presence: true

We’ll see in a minute how these predefined elements work, they are pretty easy and fast to apply in your code.

2. Make sure the user is filling out the form correctly

Another easy way to keep your database free of wrong or broken data is to display the information through the specified elements in the field, for example in our case using “number_field” which is only going to allow you to insert numbers in that space.

<label>Price:</label><br><%= f.number_field :likes %><br>

3. Be sure that the user knows that it is a website that not allows under legal age people.

Unless you develop a whole system with a scanner id that can run as a kind of security guard for your website, the only way to meet the legal requirements for access to this kind of site is by notifying the user that he or she must be over legal age to enter, and having the user declare that they are over legal age by selecting the age in your form.

Again active records have an excellent option with numericality (I know this sounds like a kind of Mortal Kombat fatality) which is going to restring the number that the user is typing.

validates :age, numericality: { greater_than: 21 }

How do we use and where do we insert a validation?

There are several places where we can insert validations: in the database, in the view, and in the models.

Although the validation in the models does not cancel the validation in the database or in the client, in this case we are going to demonstrate how it is more convenient to define the validations in the models.

app/models/user.rbclass User < ApplicationRecordvalidates :age, numericality: { greater_than: 21 }
validates :name, presence: true
end

Use predefined active records validation in your code is pretty simple, you just have to insert it into your model and call it from the controller using the “valid?” active record method.

When do validations happen?

We can usually use “valid?” to call the validations in the model from the controller and ask them if the object meets the requirements to be validated and stored in the database.

def create@user = user.new(blogger_paramsif @user.valid?# Here we are calling the validations in the model. If the user information pass the validations is going to be stored in our database@user.saveredirect_to user_path(@user)else# if not it is going to render new page with the error messagerender :newendend

What can we do if the object is not validated?

There are several ways to show the model errors in a form. The one that I am going to show next is inserting the following code in the form, which will run only if the program detects that the object has not been validated.

% if @user.errors.any? %><h2><%= @user.errors.count %> errors<h2><% @user.errors.full_messages.each do |message|%><li><%= message %></i><% end %><% end %>

This code is going to run first in your form.
If the new page is rendered because the user params wasn’t valid, “@user.errors” is going to detect which validations have been activated and will assign the correct error message for it.

Software engineering student, learning to walk into the awesome world of methods and loops

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