How to customize Devise authentication in Rails3?

Rails3
Social sharing

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.
Step#1

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

Now you can customize your methods according to the conditions.

Step#2

  • 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.

Step#1

  • 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

Step#2

  • 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’.

Step#3

  • Run the migration
rake db:migrate

Step#4

  • 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.

Your recently viewed posts:

Jayadev Das - Post Author

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 ‘About 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!

    Contact Us

    We’d love to help & work with you




    When do you want to start ?


    Enter your email address to stay up to date with the latest news.
    Holler Box

    Orange Exit pop up

    Subscribe for the latest
    trends in web and
    mobile app development
    Holler Box

    Exit pop up

    Sad to see you leaving early...

    From "Aha" to "Oh shit" we are sharing everything on our journey.
    Enter your email address to stay up to date with the latest news.
    Holler Box