Skip to content

Instantly share code, notes, and snippets.

@rbrto
Forked from kinopyo/gist:2343176
Created January 11, 2017 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rbrto/c20bfbe1f0865c90cdeec6449d267e0a to your computer and use it in GitHub Desktop.
Save rbrto/c20bfbe1f0865c90cdeec6449d267e0a 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

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