How to install devise in rails for user authentication

From the docs
Devise is a flexible authentication solution for Rails based on Warden. It:

  • Is Rack based;
  • Is a complete MVC solution based on Rails engines;
  • Allows you to have multiple roles (or models/scopes) signed in at the same time;
  • Is based on a modularity concept: use just what you really need.

Now let us start with some real stuff πŸ™‚

Create new rails project by running the following command –

rails new devise_install

Now open your GEM file (located at the base root of your folder, look for Gemfile in your app)

Add these two lines in your gemfile –

gem 'devise'

gem 'omniauth'

Now navigate to your app folder and run following command –

bundle install

Un comment bcrypt-ruby in gem file to use Active mode has_secure_password

Once you are done, run the following command –

rails generate devise:install

This command will generate an initializr to describe all devise configuration options
You MUST read those points before proceeding further.

Now we are ready to use it with MODEL.

Run the following command –

rails generate devise User

It will create a User model if not exists previously.

You will see some new files generated after the command. To this point I am interested in two files only one is under db/migrate folder i.e 20130321042152_devise_create_users.rb (in my case) and another one in app/model i.e user.rb

Now change 20130321042152_devise_create_users.rb to something like this –

And your user.rb file will go something like this –

Finally run this command –

rake db:migrate

Once all done go to http://localhost:3000/users/sign_in and you will see the devise magic πŸ™‚

NOTE – Do not forget to START your server and if you are facing some trouble with rail routes un comment the following line in your routes.rb file

match ':controller(/:action(/:id))(.:format)'

If you still not clear with the file structure you can PULL the code from github


One thought on “How to install devise in rails for user authentication

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s