Rails Authentication by using “Devise” Gem

This gem has emerged as the most used authentication solution in the Ruby community. Packaged as a Rails engine. It gives you a standard set of user sign in/out tools that are easily customization.
You will save yourself a lot of time by using this. Devise is a full-featured authentication solution which gives us all of the controller logic and form views for you. In this Blog I am going to explain how to set it up.
Step 1:Create an Application:
We Need a ROR Application, So here we creating an application by using scaffolding with Mysql Database.

$ rails new my_app -d mysql
$ cd my_app $ rails generate scaffold article name:string description:text

After this In our application we need to modify database.yml file and then specify our database,username and password.
$ rake db:create $ rake db:migrate

Modify our routes file route.rb and set root to ‘articles#index’.After that start rails server.

$ rails server

Step 2: Installing Devise:

$ gem devise install

It will install latest version of devise gem into our system. After have installed Devise, we should have to specify the Devise gem to your Gemfile and do bundle install. gem “devise” After that we will run generator by using following command:
$ rails generate devise:install

The generator will install an initializer which describes ALL Devise’s configuration options and you MUST take a look at it. When you are done, you are ready to add Devise to any of your models using the generator. Also above command generates a couple of files, an initializer and a locale file that contains all of the messages that devise needs to display.Below that it lists two manual setup steps that must be performed.

a) The first is to set the host option for the application’s mailer,We can do this by copying the line from the instructions above into the block in the development environment file.

action_mailer.default_url_options = { :host => 'localhost:3000' }

b) Second says that we must have a root route. Our application already has a root route so we don’t need to do anything there.

Step 3: Create Devise Model:
We’re going to need a User model to handle authentication and devise provides a generator for doing just that.

$ rails generate devise user

This generator creates a few interesting things: a model file, a migration and a devise_for route.Take a look at each of these files.After that run following command:

$ rake db:migrate

Step 4: Add filter in Controller:

Devise will create some helpers to use inside your controllers and views. To set up a controller with user authentication.
$ before_action :authenticate_user!

After this you restart your server and open localhost:3000 into your browser,you wil get sigin Page.
Step 5: Create a partial View:
We will create a partial to Add sign in, sign out and sign up links to your layout template. This is also to see if we’re logged in or not.
First add sign in/out links, so the appropriate one will show up depending on whether the user is already signed in. Create file app/views/layout/_login.html.erb with the following code:

<% if user_signed_in? %>
Logged in as <%= current_user.email %>.
<%= link_to ‘Edit profile’, edit_user_registration_path %> |
<%= link_to “Logout”, destroy_user_session_path, method: :delete %>
<% else %>
<%= link_to “Sign up”, new_user_registration_path %> |
<%= link_to “Login”, new_user_session_path %>
<% end %>

And invoke it within your layout (views/layouts/application.html.erb).For this add following code before or after .
With all this in place, you should be able to sign up, log in and logout from your very own Rails website.