Skip to main content

How to configure Devise gem for your Ruby on Rails Application (User Authentication)



Devise gem is used to make a simple authentication solution in rails.You no need to create authentication form it will create automatically. Like Username, Password, Signin, Signup, Forgot Password and Remember me.Please follow below steps.

10 Steps for Setup Devise gem in rails application :

 Step 1: Create a new Ruby on Rails Application

      > rails new devexam
      .
 Step 2: Open Gemfile and add the devise gem in Gemfile (devexam/Gemfile)

       gem 'devise'
     
 Step 3: Install newly added gem

      > bundle install
    
 Step 4: Create the Product form with CRUD using scaffold

      > rails g scaffold Product name:string price:integer description:text
 
 Step 5: Apply changes to database.Product table added to database with help of migration.

      > rake db:migrate

 Step 6: Add below lines to routes.rb (devexam/config/routes.rb)

       root 'products#index'

 Step 7: Setup the Devise gem

      > rails generate devise:install

 Step 8: Below command generate the USERS table

      > rails generate devise User

 Step 9: Apply the table changes to database.

      > rake db:migrate
   
    Note : Any error occur means do following steps :
 
 1. Edit the filename in below location (devexam/db/migrate               /20141126085540_devise_create_users) and add extension .rb (20141126085540_devise_create_users.rb)
 2. Now do migrate.
 
 Step 10: Open ApplicationController and add below line. (devexam/app/controller/application_controller.rb)

   class ApplicationController < ActionController::Base
     protect_from_forgery with: :exception
     before_action :authenticate_user!
   end

 Step 11: Now run your application

       > rails server

 Step 12: Add Logout and Edit profile option to your form
      Just copy and paste the below code to index.html.erb (devexam/app/views/products/index.html) or any html file
   
   <ul class="nav navbar-nav navbar-right">

     <li class="dropdown">

       <a class="dropdown-toggle" data-toggle="dropdown" href="#" id="items">

         Account <span class="caret"></span>

       </a>

       <ul class="dropdown-menu" aria-labelledby="items">

        <li>

         <% if user_signed_in? %>

           <strong>&nbsp;<%= current_user.email %></strong>

       <%= link_to 'Edit profile', edit_user_registration_path, :class => 'navbar-link'%>

        </li>

        <li>

         <%= link_to "Logout", destroy_user_session_path, method: :delete, :class => 'navbar-link'  %>

        </li>

        <% else %>

         <li>

        <%= link_to "Sign up", new_user_registration_path, :class => 'navbar-link'%>

         </li>

         <li>

          <%= link_to "Login", new_user_session_path, :class => 'navbar-link' %>

         </li>

       <% end %>

       </ul>

      </li>

    </ul>
      

Comments

Popular posts from this blog

PG::DuplicateTable: ERROR: relation "taggings" already exists in Ruby on Rails

Error: PG::DuplicateTable: ERROR:  relation "taggings" already exists Solution : Already the table present in your database.But migration also trying to create a table in database.For this reason error occurred. So try to remove the table (taggings) from your database.   postgresql :     > DROP TABLE IF EXISTS taggings;   Rails console :     > ActiveRecord::Migration.drop_table(:taggings)

How to get a YouTube video thumbnail dynamically from the YouTube API using AngularJS

In this tutorial I have explain about the get YouTube thumbnail using AngularJS <html ng-app id="YoutubeApp">   <head>     <title>       How to get a YouTube video thumbnail dynamically from the YouTube API using  AngularJS Tutorials</title>     <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.6/angular.min.js"></script>     <script src="http://code.jquery.com/jquery-1.7.2.min.js"></script>     <script>       function YoutubeController ($scope) {         $scope.todos = [];         $scope. addVideo = function() {           $scope.todos.push({text:$scope.todoText, done:false});           $scope.todoText = '';  ...

Ruby on Rails pagination using Kaminari Gem

Kaminari gem is used to create pagination in Ruby on Rails application.           Step 1: Open the terminal and Create the new application       >  rails new simplepage       Step 2: After you create the blog application, switch to its folder:             > cd simplepage             Step 3: Open and Add it to your Gemfile to following line           gem 'kaminari'           Step 4: Run the bundle install.It's used to install the gem dependencies that are  already mentioned in Gemfile               > bundle install             Step 5: create the form using scaffolding. Rails scaffolding is a quick way to generate some of the major pieces of an application.(Create, Edit, Delete, Update, Show) ...