I''m totally baffled by the relation between objects and the session and when an object is still available. It seems to me that there should be a simple session.rb file (like the session.java file in WebObjects). Right now, I''m just trying to figure out how to keep an object around, say a User object, without saving it to the db, so that I can tell at any point whether the user is logged in. I guess the answer is: session[:user] = @user where @user contains an @logged_in property. But I have no idea when I have to call: @user = session[:user] When is @user lost such that I need to grab it from the session hash again? When I change controllers? Methods? It seems that @user is an instance variable of the controller class where it''s declared so @user shouldn''t be lost between method calls within a single controller but it is. @user seems to be acting like a local variable, user. It doesn''t seem like I can access the session hash from a model for some reason. Why not? Also, If I generate a session_controller.rb does that have any kind of special status or is it just like any other controller I generate? Is there any reason to run the script/generate controller command other than to save me a minute creating files (controller, helper, view, test, etc.)? thanks, russ
On 1/8/06, Russ McBride <Russ@psyex.com> wrote:> > > I''m totally baffled by the relation between objects and the session > and when an object is still available. It seems to me that there > should be a simple session.rb file (like the session.java file in > WebObjects). > > Right now, I''m just trying to figure out how to keep an object > around, say a User object, without saving it to the db, so that I can > tell at any point whether the user is logged in. I guess the answer > is: > > session[:user] = @user > > where @user contains an @logged_in property. But I have no idea when > I have to call: > > @user = session[:user] > > When is @user lost such that I need to grab it from the session hash > again? When I change controllers? Methods? It seems that @user is > an instance variable of the controller class where it''s declared so > @user shouldn''t be lost between method calls within a single > controller but it is. @user seems to be acting like a local > variable, user.A new controller object is instantiated on every request. Perhaps that will clear stuff up. Since it''s a new controller object, the local variables aren''t kept around. So you don''t have to keep using session[:user] all the time, create a helper method in app/helpers/application.rb that looks something like def user session[:user] end Then you can use that in your controllers and views.> It doesn''t seem like I can access the session hash from a model for > some reason. Why not?Because that''s not MVC. The model shouldn''t know about the controller stuff, and session is part of the controller.> Also, If I generate a session_controller.rb does that have any kind > of special status or is it just like any other controller I generate?I think that would be fine.> Is there any reason to run the script/generate controller command > other than to save me a minute creating files (controller, helper, > view, test, etc.)?No, just saves some time and keeps stuff uniform and consistent. Why wouldn''t you use it?
Russ McBride wrote:> @user = session[:user] > > When is @user lost such that I need to grab it from the session hash > again?When a new request is started, e.g. in a before_filter. Each request creates a new controller instance, you can''t use instance variables across requests.> It doesn''t seem like I can access the session hash from a model for > some reason. Why not?Because that violates the Model-View-Controller concept.> Also, If I generate a session_controller.rb does that have any kind > of special status or is it just like any other controller I generate?This shouldn''t have any special status.> Is there any reason to run the script/generate controller command > other than to save me a minute creating files (controller, helper, > view, test, etc.)?No. -- Posted via http://www.ruby-forum.com/.
On 1/8/06, Andreas S. <f@andreas-s.net> wrote:> > Russ McBride wrote: > > @user = session[:user] > >Also I think its a better idea to store just the user''s unique id instead of the whole user object. session[:user]=@user.id I think( i am not too sure though) that session[:user]=@user will result in the entire instnace to be serialized using to_s and then stored on the client''s browser which can result in lots of memory usage on the server because of the session cache if many users are logged on. -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060109/0df842c3/attachment.html
On 1/9/06, Russ McBride <Russ@psyex.com> wrote:> > Thanks for your replies! > > Knowing that a new controller instance is created at each request is > somewhat important ;-) And Vivek, thanks for the memory usage point. > > A couple of follow-ups, if you feel like ''em otherwise I''ll post them > to the list: > > 1- It seems like at the time the application controller is > initialized (assuming there is one) a session hash should already > exist even if there are no keys in it. But I guess it''s set to nil > until a key/value is added. Is this correct? > > 2- It seems like in a controller method you should be able to set a > local variable to something, x = "blah", and then display x in the > appropriate view, <html><body> Here''s x: <%= x %>, </body><html> but > I''m only getting errors when I do this ... unless I''m missing > something obvious...Use instance variables. controller @x = "blah" view: <html><body> here''s x: <%= @x %> </body</html> local variables go away at the end of the function.
> > > 2- It seems like in a controller method you should be able to set a >> local variable to something, x = "blah", and then display x in the >> appropriate view, <html><body> Here''s x: <%= x %>, </body><html> but >> I''m only getting errors when I do this ... unless I''m missing >> something obvious... > >Use instance variables. > >controller >@x = "blah" > >view: ><html><body> here''s x: <%= @x %> </body</html> > >local variables go away at the end of the function.I guess the question is one about where the view is rendered. If there isn''t an explicit ''render'' or ''redirect'' statement at the end of the function then I gather one is inserted automatically and the view is process **after ** the function. O.k., makes sense.
Hmmm... I can''t use session[:user_id].nil? in the application controller''s initialize method because the session itself is still apparently nil when the initialize is called. Does initialize run before the session is loaded back in or something? --r> >1- It seems like at the time the application controller is >initialized (assuming there is one) a session hash should already >exist even if there are no keys in it. But I guess it''s set to nil >until a key/value is added. Is this correct? > > >Yes I guess that a session hash does exist becuase I actually use >session[:user_id].nil? to check if a user is logged on or not based >on which I take the user to the home page or to the index page-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060110/0d6ba855/attachment.html