How do you make a variable available to all views?

Another stupid question:
I have a variable that gets inserted into the main layout. Now, it would
be silly to go and set it as an instance variable for every single
method, right? But when I try to set it in the application.rb file, say
using “@variable = Variable.find(:first)”, it isn’t available to any of
my views. I tried putting the extra “@” in front of the instance
variable name to make “@@variable”, but that didn’t work either. Anybody
know what I’m doing wrong? Should I set it as a constant maybe?
Thanks

use the session. session[:myvariable] = …

On 10/12/07, Sean C. [email protected] wrote:

Another stupid question:
I have a variable that gets inserted into the main layout. Now, it would
be silly to go and set it as an instance variable for every single
method, right? But when I try to set it in the application.rb file, say
using “@variable = Variable.find(:first)”, it isn’t available to any of
my views. I tried putting the extra “@” in front of the instance
variable name to make “@@variable”, but that didn’t work either. Anybody
know what I’m doing wrong? Should I set it as a constant maybe?
Thanks

Set it from a filter in your application controller.

Isak

On 10/12/07, Sean C. [email protected] wrote:

Another stupid question:
I have a variable that gets inserted into the main layout. Now, it would
be silly to go and set it as an instance variable for every single
method, right? But when I try to set it in the application.rb file, say
using “@variable = Variable.find(:first)”, it isn’t available to any of
my views. I tried putting the extra “@” in front of the instance
variable name to make “@@variable”, but that didn’t work either. Anybody
know what I’m doing wrong? Should I set it as a constant maybe?
Thanks

Why not make a helper in app/helpers/application_rb:

def the_var
Variable.find :first
end

Then, in your layout, or in any view:

<%= h the_var.some_column %>

“global” variables like this are generally a bad idea, and should be
avoided if possible.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs