Skip to content

How To: Customize the redirect after a user edits their profile

Dimitar Dimitrov edited this page Apr 4, 2015 · 11 revisions

Normally, after a user edits their profile they are redirected to the root_path. To have Devise redirect to a custom route after an update, follow these steps:

  1. Sub-class Devise's registration controller.
  2. Override the after_update_path_for(resource) method.
  3. Configure Devise's routing.

Example subclass/override (registrations_controller.rb):

class RegistrationsController < Devise::RegistrationsController

  protected

    def after_update_path_for(resource)
      user_path(resource)
    end
end

Example routing config (in routes.rb):

devise_for :users, :controllers => { :registrations => :registrations }

The above code, will redirect the user back to the edit page, after the form submits with success. Depending on how you have configured your registrations views (if at all) you may need to move them into a different package.

Alternatively, you can define user_root in your config/routes.rb:-

as :user do
  get 'users', :to => 'users#show', :as => :user_root # Rails 3
end

The as method is an alias for devise_scope and tells Devise which resource is the route for. For example, you could use the following to redirect a user to the profile edit page after login and profile update:

as :user do
  get 'users/profile', :to => 'devise/registrations#edit', :as => :user_root
end

Where 'users#show' is the controller/action you want to redirect to.

...or (though not quite as elegant) you can just add a new match clause to your config/routes.rb:

match 'user_root' => 'users#show'
Clone this wiki locally