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
Hello Philipp
Hello from Rails
It is <%= @time %>
Cu <%= link_to "Bye!", :action => "goodbye" %>
cat app/views/say/goodbye.rhtml
Bye Philipp
Going home
Time was <%= @time %>
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
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
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
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