Trevor August
Trevor August

Reputation: 41

Ruby on Rails 5 and Devise: Overriding after_sign_out_path_for

Question: How do I make sure that my overridden after_sign_out_for method is called whenever a user logs off? I want to override this method so that I can redirect to a specific page.

Whenever a user logs out (calling the destroy method in the sessions controller) my after_sign_out_path_for method that I overrode is never called. However, I'm not having any trouble with the after_sign_in_path_for method

I've been trying to solve this problem for a few hours now, and this https://github.com/plataformatec/devise/wiki/How-To:-Change-the-redirect-path-after-destroying-a-session-i.e.-signing-out link did not work for me. I've also looked at most SO posts on the topic and seen some conflicting viewpoints.

Here is the controller method:

 def destroy
   super
 end

I have tried putting the following code in both my application and sessions controller to no avail.

protected

  def store_location_for(resource_or_scope, location)
    return nil
   end
   def stored_location_for(resource_or_scope)
    return nil
   end

  def after_sign_out_path_for(resource_or_scope)
    puts "called from AFTER SIGN OUT PATH"
    new_user_session_path
  end

This is the relevant piece of code in my routes

devise_for :users, controllers: { sessions: "users/sessions" }

Finally, this is the view code that calls the controller method:

<%= link_to 'Sign out', destroy_user_session_path(:user), :method => :delete %> 

Thanks for your time

Upvotes: 1

Views: 1217

Answers (1)

Trevor August
Trevor August

Reputation: 41

So I found an answer that wasn't mentioned in any of the other SO posts I saw. Simply call sign_out_and_redirect(current_user) and remove the call to super from destroy

This method calls after_sign_out_path_for(resource_or_scope)

Upvotes: 3

Related Questions