AuthLogic - how to determine current user id throughout the system?
I have set up AuthLogic almost exactly as per the AuthLogic example app at http://github.com/binarylogic/authlogic_example.
After someone logs in as User, they can click on links that send them away into the system and away from the users controller. This is an 开发者_StackOverflow中文版incredibly noob question, but how can I access that User's ID and other attributes from anywhere else, such as an unrelated view or unrelated controller?
An example of what I'd like to do:
#matchings controller
@matching = Matching.find_by_user_id(user.id)
You can use current_user
or @current_user
. The function that returns current_user is defined in application controller.
... 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 ...
So, you can use:
@matching = Matching.find_by_user_id(current_user.id)
or
@matching = Matching.find_by_user(current_user)
精彩评论