Forum: Ruby on Rails Caching of classes non working or Howto?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Philipp Ott (Guest)
on 2006-02-20 21:27
(Received via mailing list)
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 L. (Guest)
on 2006-02-20 21:35
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,
> Philipp

Instance 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
Philipp Ott (Guest)
on 2006-02-20 21:45
(Received via mailing list)
Hi!

Keith L. 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
Trevor S. (Guest)
on 2006-02-20 23:17
(Received via mailing list)
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
This topic is locked and can not be replied to.