# How do you make a variable available to all views?

**URL:** <https://www.ruby-forum.com/t/how-do-you-make-a-variable-available-to-all-views/117036>\
**Category:** Rails\
**Created:** [October 12, 2007, 10:30am UTC](https://www.ruby-forum.com/t/how-do-you-make-a-variable-available-to-all-views/117036 "2007-10-12T10:30:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![smc7000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@smc7000](https://www.ruby-forum.com/u/smc7000)\
**Post date:** [October 12, 2007, 10:30am UTC](https://www.ruby-forum.com/t/how-do-you-make-a-variable-available-to-all-views/117036/1 "2007-10-12T10:30:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![smc7000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@smc7000](https://www.ruby-forum.com/u/smc7000)\
**Post date:** [October 12, 2007, 10:45am UTC](https://www.ruby-forum.com/t/how-do-you-make-a-variable-available-to-all-views/117036/2 "2007-10-12T10:45:10Z")

</div>

use the session. session[:myvariable] = …

---

<div class="post-metadata">

**Author:** ![smc7000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@smc7000](https://www.ruby-forum.com/u/smc7000)\
**Post date:** [October 12, 2007, 11:37am UTC](https://www.ruby-forum.com/t/how-do-you-make-a-variable-available-to-all-views/117036/3 "2007-10-12T11:37:20Z")

</div>

On 10/12/07, Sean C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![smc7000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@smc7000](https://www.ruby-forum.com/u/smc7000)\
**Post date:** [October 12, 2007, 4:35pm UTC](https://www.ruby-forum.com/t/how-do-you-make-a-variable-available-to-all-views/117036/4 "2007-10-12T16:35:31Z")

</div>

On 10/12/07, Sean C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.
