Ruby on Rails 5 and Devise:Overriding after_sign_out_path_for

问题描述:

问:如何确保在用户注销时调用覆盖的after_sign_out_for方法?我想重写此方法,以便我可以重定向到特定页面。Ruby on Rails 5 and Devise:Overriding after_sign_out_path_for

每当用户注销(在会话控制器中调用destroy方法)我的覆盖方法我的after_sign_out_path_for永远不会被调用。不过,我并没有与after_sign_in_path_for方法

我一直想现在就解决这个问题了几个小时,这 https://github.com/plataformatec/devise/wiki/How-To:-Change-the-redirect-path-after-destroying-a-session-i.e.-signing-out链接并没有为我工作的任何麻烦。我也查看了关于该主题的大多数SO帖子,并看到了一些相互矛盾的观点。

这里是控制器方法:

def destroy 
    super 
end 

我试图把下面的代码在我的两个应用程序和会话控制器无济于事。

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 

这是我的路线相关的代码

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

最后,这是调用控制器方法的视图代码:

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

感谢您的时间

所以我找到了一个在我看到的其他SO帖子中没有提到的答案。只需拨打sign_out_and_redirect(current_user),并从销毁中删除超级呼叫

此方法呼叫after_sign_out_path_for(resource_or_scope)