Hello! I m new to rails. I m playing around with it on a SuSE 10.0 box with ruby 1.8.2 and gems and rails and postgres. rails xyz creates a nice rails application, with its directory and ruby script/server runs webrick nicely and lets me see the page on localhost:3000. I went with some tutorial to create a Hello page showing the time and a Bye-Link to link to an Bye page. cat app/controller/say_controller.rb class SayController < ApplicationController def hello @time = Time.now end def goodbye end end cat app/views/say/hello.rhtml <html> <head> <title>Hello Philipp</title> </head> <body> <b1>Hello from Rails</b1> <p> It is <%= @time %> </p> <p> Cu <%= link_to "Bye!", :action => "goodbye" %> </p> </body> </html> cat app/views/say/goodbye.rhtml <html> <head> <title>Bye Philipp</title> </head> <body> <b1>Going home</b1> <p> Time was <%= @time %> </p> </body> </html> It works without error. But in goodbye @time is always nil, empty, and it doesnt matter if I play around with config/environments/development.rb and change config.cache_classes from false to true and back, nor if I run ruby script/server -c which should cache the classes. In my understanding so far, @time in the goodbye method should be the living alive instance variable of my living view controller, right? It is living beyond the lifetime of the single request?! So it should have the value from the hello method from the invocation (and creation of the controller) before. But like I said, whatever I m doing it is always nil. What am I missing? Regards, Philipp
Keith Lancaster
2006-Feb-20 19:35 UTC
[Rails] Re: Caching of classes non working or Howto?
Philipp Ott wrote:> > It works without error. But in goodbye @time is always nil, empty, and > it doesnt matter if I play around with > config/environments/development.rb and change config.cache_classes from > false to true and back, nor if I run ruby script/server -c which should > cache the classes. In my understanding so far, @time in the goodbye > method should be the living alive instance variable of my living view > controller, right? It is living beyond the lifetime of the single > request?! So it should have the value from the hello method from the > invocation (and creation of the controller) before. But like I said, > whatever I m doing it is always nil. > > What am I missing? > > Regards, > PhilippInstance vars do not live across invocations - a new instance is created with each request (this screwed me up for a while too). Caching helps when you are accessing the same page, not the same controller. To save it, you can put it in the session, session[:time] = Time.now, and then retrieve it later. There are other ways as well. Keith -- Posted via http://www.ruby-forum.com/.
Hi! Keith Lancaster wrote:> Philipp Ott wrote: > > >> controller, right? It is living beyond the lifetime of the single >> request?! So it should have the value from the hello method from the >> >> > Instance vars do not live across invocations - a new instance is created > with each request (this screwed me up for a while too).Thank you for the revelation :-) Is any part of a rubyrails application surviving the request-loop? Or none, like in traditional cgi-programming and session-state managment handles such tasks. Could I write some ruby-threaded low frequency code withhin the application that checks something and changes the application behaviour? Regards, Philipp
Hi Philipp, your *classes* are cached but instances are not. Seeing as you''re new to rails I''ll give you a simple ''rule'' (which isn''t the *only* rule, just the simplest one until you understand how it all works well enough to break the rule): Any non-model information you want to persist across requests should be stored in the session. HTH, Trevor On 20-Feb-06, at 11:25 AM, Philipp Ott wrote:> Hello! > > I m new to rails. I m playing around with it on a SuSE 10.0 box > with ruby 1.8.2 and gems and rails and postgres. rails xyz creates > a nice rails application, with its directory and ruby script/server > runs webrick nicely and lets me see the page on localhost:3000. > > I went with some tutorial to create a Hello page showing the time > and a Bye-Link to link to an Bye page. > > cat app/controller/say_controller.rb > class SayController < ApplicationController > def hello > @time = Time.now > end > def goodbye > end > end > > cat app/views/say/hello.rhtml > <html> > <head> > <title>Hello Philipp</title> > </head> > <body> > <b1>Hello from Rails</b1> > <p> > It is <%= @time %> > </p> > <p> > Cu <%= link_to "Bye!", :action => "goodbye" %> > </p> > </body> > </html> > > cat app/views/say/goodbye.rhtml > <html> > <head> > <title>Bye Philipp</title> > </head> > <body> > <b1>Going home</b1> > <p> > Time was <%= @time %> > </p> > </body> > </html> > > > It works without error. But in goodbye @time is always nil, empty, > and it doesnt matter if I play around with config/environments/ > development.rb and change config.cache_classes from false to true > and back, nor if I run ruby script/server -c which should cache the > classes. In my understanding so far, @time in the goodbye method > should be the living alive instance variable of my living view > controller, right? It is living beyond the lifetime of the single > request?! So it should have the value from the hello method from > the invocation (and creation of the controller) before. But like I > said, whatever I m doing it is always nil. > > What am I missing? > > Regards, > Philipp > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails