2015-08-23 40 views
2

沒有使用before filtercontroller我想檢查user是否通過驗證,因爲它強制user進行驗證。 我正在尋找一種方法如何在控制器中詢問user是否已通過身份驗證,並基於此決定要從controller返回哪些內容。Rails設計檢查沒有過濾器的驗證用戶

當前代碼:

class Api::TestsController < Api::ApiController 
    before_filter :authenticate_user! 
// the filter is force me to be an authenticated user to get access to this controller 
end 

我想要什麼:

class Api::TestsController < Api::ApiController 
    def index 
    if current_user 
     // do something if the user is authenticated 
    else 
     // do something if the user not authenticated 
    end 
    end 
end 

我怎麼能實現呢? 我想要什麼:

class Api::TestsController < Api::ApiController 
    def index 
    if current_user then 
     // do something if the user is authenticated 
    else 
     // do something if the user not authenticated 
    end 
    end 
end 

我如何實現這一點?

回答

3

那好,那就用方法user_signed_in?吧。假設你的設計模型是User

class Api::TestsController < Api::ApiController 
    def index 
    if user_signed_in? 
     # do something if the user is authenticated 
    else 
     # do something if the user not authenticated 
    end 
    end 
end 
相關問題