Skip to content

Instantly share code, notes, and snippets.

@kinopyo
Created April 9, 2012 12:37
Show Gist options
  • Save kinopyo/2343176 to your computer and use it in GitHub Desktop.
Save kinopyo/2343176 to your computer and use it in GitHub Desktop.
Override Devise RegistrationsController, override redirect path after updating user

Override Devise RegistrationsController

Overview

Here I'll show you

  • How to override devise registrations_controller(related to create/update user account)
  • How to change redirect path after updating user

Override RegistrationsController

Create your own controller

I created a file under users/registrations_controller.rb.

class Users::RegistrationsController < Devise::RegistrationsController
end

Set routes

# app/config/routes.rb
devise_for :users, :controllers => {:registrations => "users/registrations"}

Move view files

I assume you already use rails generate devise:views generated devise views. Move views/devise/registrations folder to views/users

Start your own code

I guess that's it.

Override redirect path after updating user

After updated user, by default you'll be redirected to root path. I want to change the path if my user is a teacher(one of roles), here is the code.

If you check the devise source code, you'll find the update looks like this:

class Devise::RegistrationsController < ApplicationController
   #...
  protected
    # The path used after sign up. You need to overwrite this method
    # in your own RegistrationsController.
    def after_sign_up_path_for(resource)
      after_sign_in_path_for(resource)
    end

end

So now we know what method to override: after_sign_up_path_for

class Users::RegistrationsController < Devise::RegistrationsController
  def after_update_path_for(resource)
    case resource
    when :user, User
      resource.teacher? ? another_path : root_path
    else
      super
    end
  end
end

Related

http://stackoverflow.com/questions/3546289/override-devise-registrations-controller

@pauldacus
Copy link

You say "So now we know what method to override: after_sign_up_path_for", but it looks like you defined "after_update_path_for" in "Users::RegistrationsController" near the bottom of the page. Am I missing something here?

@brentcappello
Copy link

Pretty sure the author meant to use after_sign_up_path_for

class Users::RegistrationsController < Devise::RegistrationsController
  def after_sign_up_path_for(resource)
    case resource
    when :user, User
      resource.teacher? ? another_path : root_path
    else
      super
    end
  end
end

@nodox
Copy link

nodox commented Feb 19, 2017

If you override the devise controller do you no longer get the default devise logic?
Do I need to rewrite authentication logic from scratch?

@matiasmasca
Copy link

@kinopyo If you has been generate the views, I think you also you should change the _path in the forms

@hamzaaltaf
Copy link

using how can I allow user to create account without providing any password in rails 4.2 ?
Please let me know if there is any way to override password blank validation of devise?

@chrisgeek
Copy link

@hamzaaltaf, you can do that by overriding the permitted_parameters for registration.it is relatively trivial.

@happyplayer1991
Copy link

happyplayer1991 commented Aug 21, 2018

Hello, pauldacus .
Can you please help me?
I want to add one condition when login.
For example only activated user can login (in user model, 'activate' field exists)
I have used Devise gem so now I can login, register successfully,
Where can I add this condition?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment