Koren Leslie Cohen

  • About
  • Blog
  • Contact

5 comments

Ruby / Rails

Rails: Devise Redirects (and how to Customize Devise)

January 1, 2015 by Koren Leslie Cohen

devise redirects

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.

  • About
  • Latest Posts
Connect
Koren Leslie Cohen
Product manager at Facebook. Former senior product manager at Dollar Shave Club in Los Angeles and software engineer at J.Crew / Madewell in New York City. Recovering trial lawyer.
Connect
Latest posts by Koren Leslie Cohen (see all)
  • 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

Related Posts

Adding a Static Page to a Rails Application
Rails: Uploading Photos via Amazon S3 and Paperclip

Share

Facebook Google+ Twitter Pinterest Email

Comments Cancel reply

Your email address will not be published. Required fields are marked *

*

code

  1. Henry Schaumburger says

    October 13, 2015 at 5:28 pm

    Thank you for sharing this information. I found it to be very useful.

    Reply
    • Koren Leslie Cohen says

      October 13, 2015 at 9:18 pm

      Happy to help!

      Reply
  2. andrew reyes says

    October 15, 2015 at 1:05 pm

    This is the first tutorial that speaks with a female voice in my head. It’s pretty and pink.

    Reply
  3. Ben says

    July 5, 2016 at 1:14 am

    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.

    Reply
    • Koren Leslie Cohen says

      July 23, 2016 at 3:21 am

      Yep – covered that in a previous post:

      http://www.korenlc.com/rails-tutorial-authentication-with-devise/

      😉

      Reply

Back to Blog

  • GitHub
  • Instagram
  • LinkedIn
  • RSS
  • Twitter

Looking for something?

Copyright 2023 Koren Leslie Cohen