If you need assistance setting up devise, please read Rails Tutorial: Authentication with Devise.
Generate RegistrationsController
To customize devise, you should first generate a RegistrationsController which inherits from devise:
class RegistrationsController < Devise::RegistrationsController end
Modify Devise Routes
In routes.rb, add the appropriate devise routes. If you have already set up devise, you should have the following in routes.rb: devise_for :users. You will want to change this to:
devise_for :users, :controllers => { registrations: 'registrations' }
Make sure this appears before your other user routes. For example, if this appears after resources :users, your router may generate an error when you attempt to visit URLs such as /users/sign_in, as it will be looking for a user with the id “sign_in”.
Customize Devise Form
The default devise registration includes an email and password. If you want to add additional information to your devise form, such as a first name and last name, you should first make sure these attributes have been added to your user model. If they have not, you will want to generate a migration to add these attributes.
Once these attributes have been added, you want to include these params in your RegistrationsController:
class RegistrationsController < Devise::RegistrationsController private # Modified Devise params for user login def sign_up_params params.require(:user).permit(:first_name, :last_name, :email, :password, :password_confirmation) end end
You may now customize your devise registration form to include these fields in views/devise/registrations/new.html.erb:
<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %> <%= devise_error_messages! %> <div><%= f.label :email %><br /> <%= f.email_field :email, autofocus: true %></div> <div><%= f.label :password %><br /> <%= f.password_field :password, autocomplete: "off" %></div> <div><%= f.label :password_confirmation %><br /> <%= f.password_field :password_confirmation, autocomplete: "off" %></div> <div > First name<br> <%= f.text_field :first_name %> </div> <div> Last name (kept private)<br> <%= f.text_field :last_name %> </div> <div class="signup-button"> <div class="form-group form"> <button type="submit" value="Send" id="submit" class="btn btn-fancy btn-block">SIGN UP</button> </div> </div> <% end %> <%= render "devise/shared/links" %>
Control Devise Redirects
It makes sense when a user creates an account, especially when an email confirmation is required, that the user is redirected to a welcome page letting them know they need to check their email to confirm registration. Or, if a user logs in, you probably want to redirect them to a page other than the root URL.
To redirect a user after registering, add the following to your RegistrationsController:
class RegistrationsController < Devise::RegistrationsController private # Modified Devise params for user login def sign_up_params params.require(:user).permit(:first_name, :last_name, :email, :password, :password_confirmation) end def after_sign_up_path_for(resource) "/newuser" end def after_inactive_sign_up_path_for(resource) "/newuser" end end
You do not need to include the method redirect_to; this will result in an error. You will, of course, want to redirect users to whatever the appropriate page is within your application.
To control the redirects after a successful login, add the following to ApplicationController:
class ApplicationController < ActionController::Base # Prevent CSRF attacks by raising an exception. # For APIs, you may want to use :null_session instead. protect_from_forgery with: :exception private def after_sign_in_path_for(resource) "/about" end end
To see the actual devise code, and the methods you will need to overwrite in order to control your redirects, click here.
- PM Career Story - April 28, 2022
- How to Transition into Product Management - December 26, 2017
- What I’ve Learned in My First Few Months as a Product Manager - October 14, 2015
Henry Schaumburger says
Thank you for sharing this information. I found it to be very useful.
Koren Leslie Cohen says
Happy to help!
andrew reyes says
This is the first tutorial that speaks with a female voice in my head. It’s pretty and pink.
Ben says
hello Koren
does one have to completely recreate the entire RegistrationsController or can one simply change the little portion of the bit that requires changing and let inheritance take care of the rest. (i.e. all i want to do is to add a new attirbute to the strong params)?
for those reading this in the future and if you’re looking for the views – remember that they need to be generated by devise so type in the command to generate them.
Koren Leslie Cohen says
Yep – covered that in a previous post:
http://www.korenlc.com/rails-tutorial-authentication-with-devise/
😉