How to install Devise in Rails 3.x by Jay November 15, 2012 2 Minutes ReadDevise is a flexible authentication solution for Rails based on Warden. Devise handles authentication across the entire stack. It has the following features: Rack based MVC based on Rails engines Allows you to have multiple roles (or models/scopes) signed in at the same time Modularity concept: use just what you really need It is composed of 12 modules: Database Authenticatable Token Authenticatable Omniauthable Confirmable Recoverable Registerable Rememberable Trackable Timeoutable Validatable Lockable Steps to install the Devise Step#1 Add the following gem in your Gemfile gem 'devise' Then run bundle install Step#2 To invoke the Devise in your application, run the devise generator rails g devise:install The generator will install an initialize, which describes all devise’s configuration options. Step#3 Create a model “User” using devise to handle authentication. rails g devise User This generator creates a few interesting things like a file, a migration and a devise_for in route. Step#4 Run the migration rake db:migrate Step#5 Devise provides some helper methods to recognize a user after sign in and default route paths for “sign in”, “sign up” and “sign out” We can modify our ‘app/views/layout/application.html.erb’ file to allow us to “sign out”, “sign in” and “sign up” by writing the following block <div> <% 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> Configuring views Since Devise is an engine, all its views are packaged inside the devise gem. Get all the view files for devise by running the following generate command rails generate devise:views You can also configure the message language, mailer from address and other things by editing the devise config files as located in following locations devise.en.yml – config/locales devise.rb – config/initializers Now you are done to use the app with authentication! Related Posts: Usage of PDFKit with Rails 3.2.8 and Ruby 1.9.3 How to Upload a File in Rails using CarrierWave Steps to execute pagination using “Kaminari” gem in Rails3 Tags: Rails, Ruby Jayadev Das firstname.lastname@example.org Do what you do best in – that’s what I’ve always believed in and that’s what I preach. Over the past 25+ years (yup that’s my expertise ‘n’ experience in the Information Technology domain), I’ve been consulting to small, medium and large companies ‘bout Web Technologies, Mobile Future as well as on the good-and-bad of tech. Blogger, International Business Advisor, Web Technology Expert, Sales Guru, Startup Mentor, Insurance Sales Portal Expert & a Tennis Player. And top of all – a complete family man!