Ruby on Rails Validations

Ruby on Rails Validations

In continuation of previous article, where we created a basic rails application, let continue to add validations to the fields. Validations are necessary to insert the data in your application as per your expectation.

There are many validations that are available in ruby on rails. To name some of them are as follows:

  • presence
  • length
  • numericality
  • uniquness

Here we will be implementing the presence validation to make sure we do not enter blank value.

In previous article we have created an application named book store, where we were inserting the books and some of its details like name, author's name, genre and publication name. We can implement validation on name of the book and author's name.

In app/models/book.rb make following changes to define validations

No alt text provided for this image

After adding above validations when you run the server and try to add the book details without entering the name of the book or author's name, you will prompted with errors saying the blank values are not allowed as shown below

No alt text provided for this image

Here you can see that validation defined in the model is restricting you to enter invalid data. To successfully enter the data in you application, now you need to enter at least book's name and its author's name.

Customize error message

  • Now if you have noticed earlier we have just mentioned in models that the value of the field should be present to enter valid record of book. We have not mentioned any message there and still we get the error message as "Name can't be blank", which is in built error message of rails.
  • What if we need to change this error message? is there a way? Yes we can definitely change the error message by slightly tweaking the validation rules, that we have defined in models, as follows:

No alt text provided for this image

Now when you try to enter the record for book without its name, you will see following:

No alt text provided for this image

Here you can see that the default error message is changed with the custom error message that we have written in model. Also here you can see that the value of author's name is entered and when we tried to submit the form, the error of the blank author name was removed.

This is an example of basic validation in rails application. You can try to implement this validations in your application. Later we will see implementation of different types of validations of rails.

In next article you will be able to implement authentication.

Till then cheers!!!

To view or add a comment, sign in

More articles by Vishal Malukani

  • Authentication in ruby on rails

    Hello Friends. Till now we have seen to setup a basic application and applying validations in previous articles.

  • Ruby on Rails app from scratch

    Ruby on Rails framework has numerous advantages. One of them is faster development of websites.

    2 Comments

Others also viewed

Explore content categories