Hey all -
One thing I find confusing about Ruby is all the variables I have to keep track of. Now I'm reading about private and protected method filters and am about to pull out my hair. Now that I got that out of the way, here's my question/concern I found in reading chapter 8 (beginning on page 250 "Managing User Logins"):
Okay, first of all I have my defined my login method within the account_controller.rb script:
Furthermore, I followed the book and created the fetch_logged_in_user protected before_filter method:
@current_user = User.find_by_login_and_password(
session[:user_id] = @current_user.id
redirect_to :controller => 'story'
Once a user logs in, the instance variable @current_user is used to hold the current user's record (within the def login method) and then stores the id of the current user withing a session (session[:user_id]). Since the before_filter :fetch_logged_in_user is executed on every page, why are we using @current_user (overwriting the @current_user instance variable we used in the def login method) to store the user's record?
return if session[:user_id].blank?
@current_user = User.find_by_id(session[:user_id])
Maybe this isn't that serious of a question, but for some reason it's bothering me. Why did the author use the same instance variable name for both the def login method and the before_filter?