# How to use variables in your layout views?

**URL:** <https://www.ruby-forum.com/t/how-to-use-variables-in-your-layout-views/99440>\
**Category:** Rails\
**Created:** [July 22, 2006, 8:17pm UTC](https://www.ruby-forum.com/t/how-to-use-variables-in-your-layout-views/99440 "2006-07-22T20:17:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![benjohnson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3ec8ea/32.png) [@benjohnson](https://www.ruby-forum.com/u/benjohnson)\
**Post date:** [July 22, 2006, 8:17pm UTC](https://www.ruby-forum.com/t/how-to-use-variables-in-your-layout-views/99440/1 "2006-07-22T20:17:03Z")

</div>

Let’s say I wanted to find 3 products from my database and put them  
in the layouts/application.rhtml view. Where do I find these 3  
products? Do I just do it in application.rhtml?

\<% for product in Product.find(:all, :limit =\> 3) %\>

Because I was under the impression that you really don’t want to do  
this in your views. You should do this in your controllers and pass  
the result to your views. But where do I do global actions like this  
in the controllers?

Thanks for your help.

Thank You,  
Ben J.  
E: removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![benjohnson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3ec8ea/32.png) [@benjohnson](https://www.ruby-forum.com/u/benjohnson)\
**Post date:** [July 23, 2006, 12:29am UTC](https://www.ruby-forum.com/t/how-to-use-variables-in-your-layout-views/99440/2 "2006-07-23T00:29:01Z")

</div>

I’m really not sure, but you might want to get these products in a  
before\_filter? That way they’ll exist no matter which controller is  
using that layout.

Ben J. wrote:

> Let’s say I wanted to find 3 products from my database and put them  
> in the layouts/application.rhtml view. Where do I find these 3  
> products? Do I just do it in application.rhtml?
> 
> \<% for product in Product.find(:all, :limit =\> 3) %\>
> 
> Because I was under the impression that you really don’t want to do  
> this in your views. You should do this in your controllers and pass  
> the result to your views. But where do I do global actions like this  
> in the controllers?
> 
> Thanks for your help.
> 
> Thank You,  
> Ben J.  
> E: removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![benjohnson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3ec8ea/32.png) [@benjohnson](https://www.ruby-forum.com/u/benjohnson)\
**Post date:** [July 23, 2006, 12:51am UTC](https://www.ruby-forum.com/t/how-to-use-variables-in-your-layout-views/99440/3 "2006-07-23T00:51:22Z")

</div>

Ben J. wrote:

> Let’s say I wanted to find 3 products from my database and put them  
> in the layouts/application.rhtml view. Where do I find these 3  
> products? Do I just do it in application.rhtml?
> 
> \<% for product in Product.find(:all, :limit =\> 3) %\>
> 
> Because I was under the impression that you really don’t want to do  
> this in your views. You should do this in your controllers and pass  
> the result to your views. But where do I do global actions like this  
> in the controllers?

I’d put a function in application.rb

def get\_3\_products  
Product.find(:all, :limit =\> 3)  
end

And in the view:

\<% get\_3\_products.each do |product| -%\>  
\<% end -%\>

–  
– Tom M.

---

<div class="post-metadata">

**Author:** ![benjohnson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3ec8ea/32.png) [@benjohnson](https://www.ruby-forum.com/u/benjohnson)\
**Post date:** [July 24, 2006, 5:52pm UTC](https://www.ruby-forum.com/t/how-to-use-variables-in-your-layout-views/99440/4 "2006-07-24T17:52:36Z")

</div>

Ben -

Yes, you want to do the find in the controllers actions. What you  
want to do is do the find in the controller action that has the same  
name as the .rhtml file. So, if you are going to use index.rhtml to  
display the find, then you would make an action in the controller of  
that view called index. Here is some sample code:

\*controller:  
def index  
@the\_products = Product.find(:all,:limit =\> 3)  
end

\*view, index.rhtml:  
\<% for product in (0…@the\_products.length-1) %\>  
\<%= product.name %\>  
\<% end %\>

Hope that helps,  
Ben L.
