2010-05-23 63 views
0

我幾乎完全按照AuthLogic示例應用程序http://github.com/binarylogic/authlogic_example設置了AuthLogic。AuthLogic - 如何確定整個系統中的當前用戶ID?

有人以用戶身份登錄後,他們可以單擊將它們發送到系統並遠離用戶控制器的鏈接。這是一個令人難以置信的問題,但我如何從其他任何地方訪問該用戶的ID和其他屬性,如不相關的視圖或不相關的控制器?

的想什麼,我做的一個例子:

#matchings controller 
@matching = Matching.find_by_user_id(user.id) 

回答

5

您可以使用current_user@current_user。返回current_user的函數在應用程序控制器中定義。

 
... 
    private 
    def current_user_session 
    return @current_user_session if defined?(@current_user_session) 
    @current_user_session = UserSession.find 
    end 

    def current_user 
    return @current_user if defined?(@current_user) 
    @current_user = current_user_session && current_user_session.record 
    end 
... 

所以,你可以使用: @matching = Matching.find_by_user_id(current_user.id)
@matching = Matching.find_by_user(current_user)

+0

非常感謝卡延。完善! – sscirrus 2010-05-24 00:35:33

相關問題