# Controller variables in layout

**URL:** <https://www.ruby-forum.com/t/controller-variables-in-layout/106279>\
**Category:** Rails\
**Created:** [June 8, 2007, 2:12am UTC](https://www.ruby-forum.com/t/controller-variables-in-layout/106279 "2007-06-08T02:12:01Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![wickie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ba9def/32.png) [@wickie](https://www.ruby-forum.com/u/wickie)\
**Post date:** [June 8, 2007, 2:12am UTC](https://www.ruby-forum.com/t/controller-variables-in-layout/106279/1 "2007-06-08T02:12:01Z")

</div>

This is probably simple to most of the experts here, but what is the  
best way to set variables to be used by a layout? Obviously, it  
shouldn’t be in each routine in the controller as that would have to be  
duplicated in every module - very error prone. Assuming you need to set  
something based on the day of the month where would you put the code so  
that the controller’s layout could use it.

Thanks in advance  
—Michael

---

<div class="post-metadata">

**Author:** ![wickie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ba9def/32.png) [@wickie](https://www.ruby-forum.com/u/wickie)\
**Post date:** [June 8, 2007, 2:19am UTC](https://www.ruby-forum.com/t/controller-variables-in-layout/106279/2 "2007-06-08T02:19:00Z")

</div>

Probably in a before\_filter. If you really want the data available  
everywhere, put the before\_filter in application.rb. If only for a  
given controller, then implement it for that class only.

On Jun 7, 2007, at 5:12 PM, Michael S. wrote:

> Thanks in advance  
> —Michael
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> >

Steve R.  
removed\_email\_address@domain.invalid  
[http://www.calicowebdev.com](http://www.calicowebdev.com)

---

<div class="post-metadata">

**Author:** ![wickie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ba9def/32.png) [@wickie](https://www.ruby-forum.com/u/wickie)\
**Post date:** [June 8, 2007, 2:29am UTC](https://www.ruby-forum.com/t/controller-variables-in-layout/106279/3 "2007-06-08T02:29:01Z")

</div>

On 6/7/07, Michael S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> This is probably simple to most of the experts here, but what is the  
> best way to set variables to be used by a layout? Obviously, it  
> shouldn’t be in each routine in the controller as that would have to be  
> duplicated in every module - very error prone. Assuming you need to set  
> something based on the day of the month where would you put the code so  
> that the controller’s layout could use it.
> 
> Thanks in advance  
> —Michael

I think the answer partially depends on what data you are trying to  
display. If the data is static, or related to something predictable,  
like the date, I would create a helper function and create the data in  
there. There might be cases where the data you want needs to come  
from the database though, and for that I would create a before filter:

before\_filter :do\_stuff

def do\_stuff  
#stuff!  
end

You should read up on before filters, there are more options that make  
them conditional and such that can come in handy.

Read more here  
[http://api.rubyonrails.org/classes/ActionController/Filters/ClassMethods.html](http://api.rubyonrails.org/classes/ActionController/Filters/ClassMethods.html)

–  
F. Morgan Whitney

> **[Home - Blizzo](http://www.blizzo.com)**
>
> Insight. Culture. Passion. OUR STORY About Our Clients We recognize that our clients are the key element in our success and that we play an important part in helping build their business and brand. Whether our client is a small service business or a...
