Skip to content

How To: Change the redirect path after destroying a session i.e. signing out

Osazeme Usen edited this page Feb 17, 2017 · 8 revisions

In your application controller file app/controllers/application_controller.rb include the following private method (below private):

class ApplicationController < ActionController::Base
  private

  # Overwriting the sign_out redirect path method
  def after_sign_out_path_for(resource_or_scope)
    root_path
  end
end

The return value of this method is the redirect url after sign-out, so you should swap root_path to set where devise redirects the users after signing out. The method is overloading the one contained in lib/devise/controllers/helpers.rb within the gem.

You should also override method Devise::Controllers::Helpers#stored_location_for in your application controller, to return nil. This applies to after_sign_in_path_for also. YMMV.

Multiple Models

In scenarios you'll need to redirect to path based on multiple instances of models. Example.. you may have current_user and current_admin as login instances.

class ApplicationController < ActionController::Base
  private

  # Overwriting the sign_out redirect path method
  def after_sign_out_path_for(resource_or_scope)
    if resource_or_scope == :user
      new_user_session_path
    elsif resource_or_scope == :admin
      new_admin_session_path
    else
      root_path
    end
  end

In this instance, I am redirecting to user login page on user logout, and also redirecting to admin login page on admin logout.

Clone this wiki locally