___  ___    _ _    _  _ _____   _____
 / __|/ _ \  | | |  | || |_ _\ \ / / __|
| (_ | (_) | |_  _| | __ || | \ V /| _|
 \___|\___/    |_|  |_||_|___| \_/ |___|

 --- A GOPHER-LIKE INTERFACE FOR HIVE BLOCKCHAIN ---

How to add validation in ruby on rails (#part3)

BY: @amn | CREATED: March 31, 2018, 10:09 a.m. | VOTES: 11 | PAYOUT: $0.23 | [ VOTE ]

What Will I Learn?

Requirements

Difficulty

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.

 localhost:3000/users/sign_up

[IMAGE: https://cdn.utopian.io/posts/c5e9fc0d3fe54b273010c8de7b1743fc5507image.png]

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
    validates :first_name, presence: { message: "is required." }
    validates :last_name, presence: { message: "is required." }
If you want to validate individual fields with the custom message that you can use this type of format
Model-level validation is very important because the user can bypass the front end validations

[IMAGE: https://cdn.utopian.io/posts/fa40fef002b27424c4e54450dd277e744dd9image.png]

[IMAGE: https://cdn.utopian.io/posts/b8408ab5a3aceb50c1020264753a9840e9f7image.png]

rake db:rollback

[IMAGE: https://cdn.utopian.io/posts/a107bbe85f821a8ab00f22423dba845e0202image.png]

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
rake db:migrate
mysql -u root -p
show database;
It will show all your MySQL database on your system
use  "your database name" ;
Update users set first_name = null;

[IMAGE: https://cdn.utopian.io/posts/c86a1746a2d386b18e20d0b6955922a6426bimage.png]

So that's all for this tutorial. we successfully validate our app and no one can bypass it without filling the necessary fields

Curriculum

Posted on Utopian.io - Rewarding Open Source Contributors

TAGS: [ #utopian-io ] [ #tutorials ] [ #rails ] [ #steemstem ] [ #validations ]

Replies

@mcfarhat | April 3, 2018, 2:13 p.m. | Votes: 2 | [ VOTE ]

Thank you for your contribution, yet it cannot be approved because it does not follow the Utopian Rules.
* The title of your contribution is misleading. You are talking about validations on text fields, not user authentication.
* You mentioned several items that the user will learn, yet they are not discussed in the tutorial, such as back end validation.
* Your examples are not well explanatory, and you do not explicitly show nor highlight properly how and where the code will be changed, not give exact samples of how the code will eventually function and look.
Please keep these notes in mind when writing your next tutorials.

You can contact us on Discord.
[utopian-moderator]

@amn | April 3, 2018, 5:06 p.m. | Votes: 0 | [ VOTE ]

Thanks for reviewing this post. As per your suggestion I updated the content. Please review it once again

@utopian.tip | April 4, 2018, 9:14 a.m. | Votes: 1 | [ VOTE ]

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!

@minnowsupport | April 4, 2018, 4:20 p.m. | Votes: 0 | [ VOTE ]

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 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.
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.

[ BACK TO TRENDING ] [ BACK TO MENU ]
CMD>