ssc*_*rus 0 variables ruby-on-rails authlogic
我几乎完全根据http://github.com/binarylogic/authlogic_example上的AuthLogic示例应用程序设置了AuthLogic .
在有人以用户身份登录后,他们可以单击将它们发送到系统并远离用户控制器的链接.这是一个令人难以置信的noob问题,但我如何从其他任何地方访问用户的ID和其他属性,例如无关的视图或不相关的控制器?
我想做的一个例子:
#matchings controller
@matching = Matching.find_by_user_id(user.id)
Run Code Online (Sandbox Code Playgroud)
你可以使用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)