You will learn how to add validation in Ruby on Rails Project such as in forms, models and in database.
You will learn how to migrate and rollback your migrations in ruby on rails project
You will learn some basic MySQL command
Requirements
OS: Ubuntu/Mac OS
A text editor like sublime
Basic understanding about Ruby on Rails
And for better understanding must refer my previous tutorials (link added to the curriculum)
Difficulty
Intermediate
Tutorial Contents
Hello and welcome to the next part of the tutorial. In this tutorial, we will learn how to add validations in our project.
But before that one question may arise that why we use validations?
The answer is that to ensure that only valid and right data should be saved into the database.
We can add severals levels of validations in our project. We will go through every aspect of validation in this tutorial, so let's start the tutorial.
Firstly go to the rails app and start the server in the terminal, that was build in my previous tutorials
And also the open project in a text editor (Sublime text)
Enter the following URL in the browser
localhost:3000/users/sign_up
You will see the sign-up form as seen below
When you fill up the form details except for user first and last name, the app does not give you an error and it will save the user normally, see below
You can see that new user is created but without its name, because we did not add any validations for that
So let's start adding the validations,
Firstly we will add validation in models so that required field's can not be saved blank, for that
go to app > models > user.rb and save the following code
validates_presence_of:first_name,:last_name
If you want to validate many fields in one line that you can use this type of format