2017-01-10 30 views
0

我在我的控制器中有before_action :logged_in_user,如果沒有current_user,它將重定向到login_path設置註銷...有沒有一種方法來檢查控制器操作是否包含before_action

我在如何在我的應用程序中設置註銷(銷燬會話)的邏輯掙扎。

如果用戶是在那裏它們被不需要logged_in?一個頁面上,我想註銷只是redirect_to :back(保持這種頁),因爲它不會影響當前頁面觀看。

如果他們需要,他們是logged_in?,我想他們是redirect_to :root_url一個頁面上,因爲否則會redirect_tologin_path這是尷尬,因爲他們只是logged_out。

因此,在僞代碼基本上我要做到以下幾點:

redirect_to :back 
    unless :back controller:action >> before_action :logged_in_user 
    then redirect_to root_url 

回答

0
SessionsController 

    def destroy 
    destroy_location 
    log_out if logged_in? 
    redirect_logout 
    end 

    def destroy_location 
    path = ["/feed", "/friends", "/saved_articles", "/favorites"] 
    if path.any?{|word| URI(request.referrer).path == word } 
     session[:exit] = root_url 
    end  
    end 

    def redirect_logout 
    redirect_to(session[:exit] || :back) 
    session.delete(:exit) 
    end 

這工作而很好!

相關問題