SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member skrolikowski's Avatar
    Join Date
    Nov 2006
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Retrieving the Current User Filter

    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:
    Code:
    def login
      if request.post?
        @current_user = User.find_by_login_and_password(
          params[:login], params[:password])
         unless @current_user.nil?
           session[:user_id] = @current_user.id
           redirect_to :controller => 'story'
         end
      end
    end
    Furthermore, I followed the book and created the fetch_logged_in_user protected before_filter method:
    Code:
    before_filter :fetch_logged_in_user
    protected
    def fetch_logged_in_user
      return if session[:user_id].blank?
      @current_user = User.find_by_id(session[:user_id])
    end
    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?

    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?

  2. #2
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why not? If the user isn't logged in the @current_user variable should be blank:

    Code:
      return if session[:user_id].blank?
    So the login method doesn't overwrite the variable because it isn't set yet.

  3. #3
    SitePoint Member skrolikowski's Avatar
    Join Date
    Nov 2006
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Maybe my point isn't that serious, but I tried the def login method like so:
    Code:
    def login
      if request.post?
        @current_user1 = User.find_by_login_and_password(
          params[:login], params[:password])
         unless @current_user1.nil?
           session[:user_id] = @current_user1.id
           redirect_to :controller => 'story'
         end
      end
    end
    Changing the @current_user instance variable to @current_user1 and nothing changed. I was just wondering why the author would choose to use the same name for a variable...

  4. #4
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think the code is clearer this way. The variable contain the same kind of object in both methods.

    If I define these prodecures:

    Code:
    def double(number)
      number * 2
    end
    
    def halve(number)
      number / 2
    end
    It is strange that I used the same variable for both procedures?

  5. #5
    SitePoint Member skrolikowski's Avatar
    Join Date
    Nov 2006
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay, I understand.
    Plus just having one instance variable @current_user takes less memory than using two (@current_user and @current_user1).

    Thanks Fenrir2


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •