2010-05-23 61 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

相关问题