How to install Devise in Rails 3.x
Devise 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
Steps to install the Devise
Add the following gem in your Gemfile
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.
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.
Run the migration
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>
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!
307 total views, 8 views today