3

When someone is logging into my application, I use:

def create
    @user = User.authenticate(params[:email], params[:password])

    [...]
end

Ok, then, when someone is logging out:

def destroy
    user = User.find_by_id(session[:user_id])

    [...]
end

Knowledge

As far as I know, variable scopes work based on a scope, at least on Ruby (on Rails).

Our friend said:

In case of controllers, it present for that HTTP request alone, the object and the instance variables.

Ok. My variable scope created on create method is useless for destroy method, but I was thinking about the subject and the following question appears: There's a way to preserve @user for that controller at all, regardless of the HTTP request?

I mean, @ in this case seems useless to me because its not flexible. I don't know, just sounds strange for me I can't reuse it when I want to.

3
  • 1
    An instance variable (starts with @) is persistent across the existence of a specific class instance. The the last set value of @user should still exist when destroy is called. If it is a different instance of the controller class, it will have its own value for @user. In the statement user = ... in destroy, the variable user is strictly local to the destroy method. Commented Feb 6, 2014 at 11:52
  • 2
    That's how the web works and why http is a 'stateless protocol'. You must understand that you are not starting to run a program and stop it when your user logs out. But you 'restart' the program for every single request. There is no state (but what you put in the session storage). @ in this case means that your view can use this data (which in the Ruby context means that Rails is doing some tricks to get it handed over there). Actually the Rails instance that handles the create and the one that handles the destroy could easily run on two physically different servers! Commented Feb 6, 2014 at 11:53
  • Guys, turn your comments into an answer. Both of you clarified my mind. Commented Feb 6, 2014 at 11:54

1 Answer 1

7

That's how the web works and why http is a 'stateless protocol'. You must understand that you are not starting to run a program and stop it when your user logs out. But you 'restart' the program for every single request. It's a new instance, a new process that knows nothing of the last one and for sure shares no memory with it. Actually the Rails instance that handles the create and the one that handles the destroy could easily run on two physically different servers!

There is no state (but what you put in the session storage or the URL params). @ in this case means that your view can use this data (which in the Ruby context means that Rails already is doing some tricks to get it handed over there, since these are two different classes and the view would otherwise not know anything about the controllers instance variables).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.