2016-12-26 103 views
0

問:如何確保在用戶註銷時調用覆蓋的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 %> 

感謝您的時間

回答

0

所以我找到了一個在我看到的其他SO帖子中沒有提到的答案。只需撥打sign_out_and_redirect(current_user),並從銷燬中刪除超級呼叫

此方法呼叫after_sign_out_path_for(resource_or_scope)