What Will I Learn?
<ul>
<li>You will learn how to add validation in Ruby on Rails Project such as in forms, models and in database.
<li>You will learn how to migrate and rollback your migrations in ruby on rails project
<li>You will learn some basic MySQL command
<h4>Requirements
<ul>
<li>OS: Ubuntu/Mac OS
<li>A text editor like sublime
<li>Basic understanding about Ruby on Rails
<li>And for better understanding must refer my previous tutorials (link added to the curriculum)
<h4>Difficulty
<ul>
<li>Intermediate
<h4>Tutorial Contents
<p dir="auto">Hello and welcome to the next part of the tutorial. In this tutorial, we will learn how to add validations in our project.
<p dir="auto">But before that one question may arise that why we use validations?<br />
The answer is that to ensure that only valid and right data should be saved into the database.
<p dir="auto">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.
<ul>
<li>Firstly go to the rails app and start the server in the terminal, that was build in my previous tutorials
<li>And also the open project in a text editor (Sublime text)
<li>Enter the following URL in the browser
<pre><code> localhost:3000/users/sign_up
<ul>
<li><p dir="auto">You will see the sign-up form as seen below<br />
<img src="https://images.hive.blog/768x0/https://cdn.utopian.io/posts/fd4a7002c57881292f33d43d2d1dc00d7e3aimage.png" alt="image.png" srcset="https://images.hive.blog/768x0/https://cdn.utopian.io/posts/fd4a7002c57881292f33d43d2d1dc00d7e3aimage.png 1x, https://images.hive.blog/1536x0/https://cdn.utopian.io/posts/fd4a7002c57881292f33d43d2d1dc00d7e3aimage.png 2x" />
<li><p dir="auto">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
<p dir="auto"><img src="https://images.hive.blog/768x0/https://cdn.utopian.io/posts/c5e9fc0d3fe54b273010c8de7b1743fc5507image.png" alt="image.png" srcset="https://images.hive.blog/768x0/https://cdn.utopian.io/posts/c5e9fc0d3fe54b273010c8de7b1743fc5507image.png 1x, https://images.hive.blog/1536x0/https://cdn.utopian.io/posts/c5e9fc0d3fe54b273010c8de7b1743fc5507image.png 2x" />
<h6><em>You can see that new user is created but without its name, because we did not add any validations for that
<h4>So let's start adding the validations,
<p dir="auto">Firstly we will add validation in models so that required field's can not be saved blank, for that
<ul>
<li><b><em>go to app > models > user.rb and save the following code
<pre><code> validates_presence_of :first_name, :last_name
<h6>If you want to validate many fields in one line that you can use this type of format
<pre><code> validates :first_name, presence: { message: "is required." }
validates :last_name, presence: { message: "is required." }
<h6>If you want to validate individual fields with the custom message that you can use this type of format
<ul>
<li>And if the user tried to submit the form, the app will show error like this<br />
<img src="https://images.hive.blog/768x0/https://cdn.utopian.io/posts/ae057b25cc1fe680f2b052cf2f08aa4a0fbdimage.png" alt="image.png" srcset="https://images.hive.blog/768x0/https://cdn.utopian.io/posts/ae057b25cc1fe680f2b052cf2f08aa4a0fbdimage.png 1x, https://images.hive.blog/1536x0/https://cdn.utopian.io/posts/ae057b25cc1fe680f2b052cf2f08aa4a0fbdimage.png 2x" />
<h6><em>Model-level validation is very important because the user can bypass the front end validations
<ul>
<li><p dir="auto">Now we will add validation in forms so that required field's can not be submit it blank, for that
<li><p dir="auto">Just add <b>required: true in the input type fields.
<p dir="auto"><img src="https://images.hive.blog/768x0/https://cdn.utopian.io/posts/fa40fef002b27424c4e54450dd277e744dd9image.png" alt="image.png" srcset="https://images.hive.blog/768x0/https://cdn.utopian.io/posts/fa40fef002b27424c4e54450dd277e744dd9image.png 1x, https://images.hive.blog/1536x0/https://cdn.utopian.io/posts/fa40fef002b27424c4e54450dd277e744dd9image.png 2x" />
<ul>
<li>And now if the user submits the form with a blank username then the following error will be shown
<p dir="auto"><img src="https://images.hive.blog/768x0/https://cdn.utopian.io/posts/b8408ab5a3aceb50c1020264753a9840e9f7image.png" alt="image.png" srcset="https://images.hive.blog/768x0/https://cdn.utopian.io/posts/b8408ab5a3aceb50c1020264753a9840e9f7image.png 1x, https://images.hive.blog/1536x0/https://cdn.utopian.io/posts/b8408ab5a3aceb50c1020264753a9840e9f7image.png 2x" />
<ul>
<li>And lastly we will add validation at database
<li>Database validations are required when someone tried to alter the database directly
<li>You see if the form is submitted without email, it gives the error because devise by default add some database level validations
<li>But we added the username field later and it is already migrated so for that we have to rollback our database
<li>Stop the server and run the following command
<pre><code>rake db:rollback
<ul>
<li>It shows the info as shown below
<p dir="auto"><img src="https://images.hive.blog/768x0/https://cdn.utopian.io/posts/a107bbe85f821a8ab00f22423dba845e0202image.png" alt="image.png" srcset="https://images.hive.blog/768x0/https://cdn.utopian.io/posts/a107bbe85f821a8ab00f22423dba845e0202image.png 1x, https://images.hive.blog/1536x0/https://cdn.utopian.io/posts/a107bbe85f821a8ab00f22423dba845e0202image.png 2x" />
<ul>
<li>Now go to your <b> <em>app > db > migrate > your add fields to user migrate name file and change the code as below and save it
<pre><code>class AddFieldsToUser < ActiveRecord::Migration
def change
add_column :users, :first_name, :string, null: false
add_column :users, :last_name, :string, null: false
add_column :users, :mobile_number, :integer
end
end
<ul>
<li><p dir="auto">Or you can refer image below<br />
<img src="https://images.hive.blog/768x0/https://cdn.utopian.io/posts/14038bd1f083e49aec202e03924e4e278296image.png" alt="image.png" srcset="https://images.hive.blog/768x0/https://cdn.utopian.io/posts/14038bd1f083e49aec202e03924e4e278296image.png 1x, https://images.hive.blog/1536x0/https://cdn.utopian.io/posts/14038bd1f083e49aec202e03924e4e278296image.png 2x" />
<li><p dir="auto">Now run the following command to migarte our database
<pre><code>rake db:migrate
<ul>
<li>It will migrate the database
<li>And now if someone tried to save the username blank directly from the database, it won't be able to do that
<li>So let's have a try on it, open the terminal and go to your project path and enter into MySQL console
<pre><code>mysql -u root -p
show database;
<h6><em>It will show all your MySQL database on your system
<ul>
<li>Now select your database
<pre><code>use "your database name" ;
<ul>
<li>Now try to change the username in the user table
<pre><code>Update users set first_name = null;
<ul>
<li>You will see the error like as shown in below image
<p dir="auto"><img src="https://images.hive.blog/768x0/https://cdn.utopian.io/posts/c86a1746a2d386b18e20d0b6955922a6426bimage.png" alt="image.png" srcset="https://images.hive.blog/768x0/https://cdn.utopian.io/posts/c86a1746a2d386b18e20d0b6955922a6426bimage.png 1x, https://images.hive.blog/1536x0/https://cdn.utopian.io/posts/c86a1746a2d386b18e20d0b6955922a6426bimage.png 2x" />
<p dir="auto">So that's all for this tutorial. we successfully validate our app and no one can bypass it without filling the necessary fields
<h4>Curriculum
<ul>
<li><span><a href="https://utopian.io/utopian-io/@amn/how-to-authenticate-user-in-rails-using-devise-part1" target="_blank" rel="nofollow noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">https://utopian.io/utopian-io/@amn/how-to-authenticate-user-in-rails-using-devise-part1
<li><span><a href="https://utopian.io/utopian-io/@amn/how-to-authenticate-user-in-rails-using-devise-part2" target="_blank" rel="nofollow noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">https://utopian.io/utopian-io/@amn/how-to-authenticate-user-in-rails-using-devise-part2
<p dir="auto"><br /><hr /><em>Posted on <a href="https://utopian.io/utopian-io/@amn/how-to-authenticate-user-in-rails-using-devise-part3" target="_blank" rel="nofollow noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">Utopian.io - Rewarding Open Source Contributors<hr /><p>
Thank you for your contribution, yet it cannot be approved because it does not follow the Utopian Rules.
Please keep these notes in mind when writing your next tutorials.
You can contact us on Discord.
[utopian-moderator]
Hey @mcfarhat, I just gave you a tip for your hard work on moderation. Upvote this comment to support the utopian moderators and increase your future rewards!
Thanks for reviewing this post. As per your suggestion I updated the content. Please review it once again
Peace, Abundance, and Liberty Network (PALnet) Discord Channel. It's a completely public and open space to all members of the Steemit community who voluntarily choose to be there.Congratulations! This post has been upvoted from the communal account, @minnowsupport, by amn from the Minnow Support Project. It's a witness project run by aggroed, ausbitbank, teamsteem, theprophet0, someguy123, neoxian, followbtcnews, and netuoso. The goal is to help Steemit grow by supporting Minnows. Please find us at the
If you would like to delegate to the Minnow Support Project you can do so by clicking on the following links: 50SP, 100SP, 250SP, 500SP, 1000SP, 5000SP.
Be sure to leave at least 50SP undelegated on your account.