SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Guru Skyblaze's Avatar
    Join Date
    Jul 2005
    Location
    Italy
    Posts
    734
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Rails instantiation of a controller's object...

    Is a controller's object instantiated by rails on every page/action request (so on every action that old controller's object is deleted form memory) or there is one instance of that controller only once in the first request of any page/action of that particular controller?
    If it is the latter we can place code that set any particular variable that we want to use on any action in a before filter right? So that variable isn't lost on subsequent different action requests. Is that right?

    That is also why in the sitepoint's rails book on chapter 8 in the login system when the user logs out it is deleted the session variable associated with the user_id with also the instance variable setted in a before filter with a user object.

  2. #2
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You cannot do this. The web server can run many Rails requests at once, (possibly spread across multiple computers). The session is different: it's stored in the database or in a file (or somewhere else), and it's reloaded on every request. It's also user-specific.

  3. #3
    SitePoint Guru Skyblaze's Avatar
    Join Date
    Jul 2005
    Location
    Italy
    Posts
    734
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Fenrir2 View Post
    You cannot do this. The web server can run many Rails requests at once, (possibly spread across multiple computers). The session is different: it's stored in the database or in a file (or somewhere else), and it's reloaded on every request. It's also user-specific.
    i'm not talking about sessions ...where did i mention sessions?

    anyway the controller's object is instantied on every request right? so when it is reinstantied all variables are reset so why in the login system we manually need to assign nil to a variable that has been set to the user object in a before method in application.rb?

  4. #4
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Skyblaze View Post
    i'm not talking about sessions ...where did i mention sessions?
    ...
    That is also why in the sitepoint's rails book on chapter 8 in the login system when the user logs out it is deleted the session variable associated with the user_id with also the instance variable setted in a before filter with a user object.
    ;-)

    anyway the controller's object is instantied on every request right? so when it is reinstantied all variables are reset so why in the login system we manually need to assign nil to a variable that has been set to the user object in a before method in application.rb?
    True. If it's an @var variable you don't need to assign nil to it. BUT, maybe there is an after filter that stores the @user object in the session?

  5. #5
    SitePoint Guru Skyblaze's Avatar
    Join Date
    Jul 2005
    Location
    Italy
    Posts
    734
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Fenrir2 View Post
    ...


    ;-)



    True. If it's an @var variable you don't need to assign nil to it. BUT, maybe there is an after filter that stores the @user object in the session?
    no, it is an instance variable declared and assigned in a before method in application.rb and it is assigned only if it is found a session variable that contains the user id logged in so any next request of a user not logged in (or the same user that logged out) doesn't have that instance variable 'cause the session variable isn't found.....so i don't understand wht to bother to assign it a nil value when the user logs out and the session variable is set to nil.

    here is the code with the instance variable @current_user:

    Code:
    def fetch_logged_in_user
        return if session[:user_id].blank?
        @current_user = User.find_by_id(session[:user_id])
      end
    ...and when the user logs out the session variable session[:user_id] is set to nil:
    Code:
    def logout
        session[:user_id] = @current_user = nil    
      end

  6. #6
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, if the method doesn't redirect, maybe @current_user should be nil for the template?

    Try to remove @current_user from the logout method and run your unit tests (if you have any) or test the application trough your browser. I think you can safely remove this variable, but you have to test to be sure.

  7. #7
    SitePoint Guru Skyblaze's Avatar
    Join Date
    Jul 2005
    Location
    Italy
    Posts
    734
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Fenrir2 View Post
    Well, if the method doesn't redirect, maybe @current_user should be nil for the template?

    Try to remove @current_user from the logout method and run your unit tests (if you have any) or test the application trough your browser. I think you can safely remove this variable, but you have to test to be sure.
    yes maybe it is so 'cause the before filter above is executed also before the logout action and sets that user instance variable so for the logout template we need to reset it to nil.....


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
  •