How to customize Devise authentication in Rails3?

How to customize Devise authentication in Rails3?

2 Minutes Read

Rails31Devise is an authentication solution for Rails application development. Since devise is an engine and all the files created by it are packaged inside the gem. We need to invoke some generators in order to customize it according to our choice.

Configuring views:

  • If you want to modify the views generated by devise, then you just need to invoke the following generator, and it will copy all views to your application.
rails generate devise:views
  • If you have more than one role in your application (such as “User” and “Admin”), Devise offers an easy way to customize views. Just write the following line inside your “config/initializers/devise.rb”
config.scoped_views = true
  • Now you will be able to have views based on the role like “users/sessions/new” and “admins/sessions/new”. You can also use the generator to generate scoped views like below:
rails generate devise:views users

Configuring controllers:

If the customization at the views level is not enough, you can customize the controller generated by devise using following steps.

  • Create a custom controller
class Users::SessionsController < Devise::SessionsController
[Your code goes here]

Now you can customize your methods according to the conditions.


  • Now tell the route to use this controller in “config/routes.rb”
devise_for :users, :controllers => { :sessions => "users/sessions" }

ustomizing Error Messages:

Devise has its own error messages that are shown when something goes wrong. All of these messages are stored in a locale file (config/locales/devise.en.yml), making it easy to maintain them. Here you can see the list of error messages created by devise and you can customize them according to your choice.

Customizing Registration process:

You can also customize the registration process of devise. Here is an example to explain the customization of Registration process where devise sends an activation email automatically after a new user registration happens.


  • Modify the “users” migration file created by devise. Uncomment the block of fields under Confirmable.
t.string   :confirmation_token
t.datetime :confirmed_at
t.datetime :confirmation_sent_at
t.string   :unconfirmed_email


  • Customize the “User” model like below:
devise :database_authenticatable, :registerable,:recoverable, :rememberable, :trackable, :validatable, :confirmable

We have added “:confirmable” module for devise to the model. Confirmable is responsible to verify if an account is already confirmed to sign in, and to send emails with confirmation instructions. Confirmation instructions are sent to the user email after creating a record.

Then add the field names that are defined in step#1 to the ‘attr_accessible’.


  • Run the migration
rake db:migrate


  • Now set the default URL for according to your requirement in “config/enviroments/development.rb”
config.action_mailer.default_url_options = { :host => 'localhost:3000' }

Now if you register for a new user then devise will send a confirmation email with required instructions.

Jayadev Das

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!

No Comments

Post A Comment

Exit pop up

Before You Go...!

Subscribe to Andolasoft Blog

Stay up to date with the latest techniques of web & mobile app development service tips and our SaaS and open source product news.
Holler Box