Ruby on Rails Sunday, July 31, 2011

> The purpose of this line is to create an assignment to current user
> and the existence of the instance variable @current_user is to
> "permanently" stored, as long as it is needed (not as a local
> variable) since there's no model?
>

@ variables can be seen in all the class's methods (that is from ruby),
and in rails you have classes with names like SessionsController, and
the methods defined in the class are called actions. Rails arranges for
@ variables to be seen in any views as well.


In my previous post, I said:

>> '@' variables persist as long as the sess_controller object
>> still exists.

However, the web is a very ephemeral place to do business, i.e, things
exist for only a short period of time before being destroyed. For
instance, a browser sends a request to your rails app, which then
returns a response in the form of an html page, which usually happens in
few seconds--and then everything gets destroyed. Afterwards, the
browser doesn't remember communicating with your rails app, and your
rails app doesn't remember ever communicating with that browser.

As far as I know, because I am a rails beginner too (and I happen to be
at the same spot as you in the book), the sess_controller object, which
rails uses to call an action in response to a particular url, is
destroyed as soon as the application sends any html page back to the
browser.

The @current_user variable appears in this method:

def current_user=(user)
@current_user = user
end

And @ variables attach themselves to whatever is currently self, which
as discussed in my previous post will be this object:

sess_controller = SessionsController.new

So knowledge of the current user is destroyed when the sess_controller
object is destroyed, and that happens only a few seconds after the
browser
initially sends its request--hardly anything permanent.

The trick is to store a permanent cookie on the browser. When a browser
sends any request to your rails app, it adds all cookies that were
stored by your app. The code you are currently studying in the
book stores a cookie on the browser. Presumably, later in the book, the
app will always check any browser request to see if the user is 'signed
in' or 'signed out', which means: did the browser send a 'signed in'
cookie in the request, or was such a cookie absent.

--
Posted via http://www.ruby-forum.com/.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

No comments:

Post a Comment