Friday 21 February 2014

Devise Gem Install in Ruby on Rails application follow the Step by step process:

Step 1:- Open Rails application in Gemfile add a reference to devise

               gem 'devise'  

Step 2:- To install The gem dependencies run the command 

               bundle install  

Step 3:- The Next Step to Devise installation generator in ruby on rails application. Here it show list to do Manual setup steps as follow:

             rails generate devise:install  
    create  config/initializers/devise.rb
       create  config/locales/devise.en.yml

===============================================================================
    Some setup you must do manually if you haven't yet:
  1. Ensure you have defined default url options in your environments files. Heris an example of default_url_options appropriate for a development environment in                  config/environments/development.rb:      
    config.action_mailer.default_url_options = { :host => 'localhost:3000' }   

In production, :host should be set to the actual host of your application.

  2. Ensure you have defined root_url to *something* in your config/routes.rb.
     For example:
      root :to => "home#index"

  3. Ensure you have flash messages in app/views/layouts/application.html.erb.
     For example:
       <p class="notice"><%= notice %></p>
       <p class="alert"><%= alert %></p>  

  4. If you are deploying on Heroku with Rails 3.2 only, you may want to set:
       config.assets.initialize_on_precompile = false
    
    On config/application.rb forcing your application to not access the DB
     or load models when precompiling your assets.

  5. You can copy Devise views (for customization) to your app by running:
       rails g devise:views
===============================================================================

Step 4:-  Create Devise user model run the command in terminal

         rails generate devise user  

       Here devise provides a generator creates user model to handle the authentication.

Step 5:- Create a database for users run command in terminal

               rake db:migrate  
Run a command in terminal to know the list of routes in application 
When divise gem installed routes are having as follow
rake routes
new_user_session_path GET /users/sign_in(.:format) devise/sessions#new
user_session_path POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session_path DELETE /users/sign_out(.:format) devise/sessions#destroy
user_password_path POST /users/password(.:format) devise/passwords#create
new_user_password_path GET /users/password/new(.:format) devise/passwords#new
edit_user_password_path GET /users/password/edit(.:format) devise/passwords#edit
PATCH /users/password(.:format) devise/passwords#update
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration_path GET /users/cancel(.:format) devise/registrations#cancel
user_registration_path POST /users(.:format) devise/registrations#create
new_user_registration_path GET /users/sign_up(.:format) devise/registrations#new
edit_user_registration_path GET /users/edit(.:format) devise/registrations#edit
PATCH /users(.:format) devise/registrations#update
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy   
Step 6:- 
To improve the authentication setup we can do some modification in the rails 4 layout folder in application file paste the following code before that displays the flash message:
layouts/application.html.erb

<div id= "user_nav">
  <% if user_signed_in? %>
     Signed in as <%= current_user.email %>. Not you?
     <%= link_to "Sign out", destroy_user_session_path, :method => :delete %>
   <% else %>
     <%= link_to "Sign up", new_user_registration_path %> or
     <%= link_to "Sign in", new_user_session_path %>
   <% end %>
 </div>

Hope the above steps helpful Devise gem install rail 4 application
Errors:
Here face one error when try to sign out user shows the following issue.

Routing Error
No route matches [GET] "/users/sign_out"

To overcome this error i just change the  configuration in devise.rb file in
      config/initializers/devise.rb:

Replace :
            config.sign_out_via = :delete  with 
            config.sign_out_via = :get

I hope that, this is helpful to resolve the sign out issue No route matches [GET] "/users/sign_out" 


6 comments: