# Layouts - how to render partials from multiple controllers

**URL:** <https://www.ruby-forum.com/t/layouts-how-to-render-partials-from-multiple-controllers/77306>\
**Category:** Rails\
**Created:** [November 26, 2006, 7:11pm UTC](https://www.ruby-forum.com/t/layouts-how-to-render-partials-from-multiple-controllers/77306 "2006-11-26T19:11:57Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![richard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f0a364/32.png) [@richard](https://www.ruby-forum.com/u/richard)\
**Post date:** [November 26, 2006, 7:11pm UTC](https://www.ruby-forum.com/t/layouts-how-to-render-partials-from-multiple-controllers/77306/1 "2006-11-26T19:11:57Z")

</div>

I’m not understand how to piece my whole application together.

Basically I have a home page for a restaurants website which will  
display a search facility, a login box, and some stats.

Now my first problem is not knowing which controller this should be the  
layout for, because the only part of this page that would be common to  
all other pages is the header and that is it: see  
[http://www.eatnewcastle.com/test/layout.htm](http://www.eatnewcastle.com/test/layout.htm)

My second problem is that the login box functionality is part of an  
account controller, the search box functionality is part of a search  
controller etc So how can I render partials from all these different  
controllers in this one layout?

I hope this makes sense. Please reply if any aspect is confusing and I  
will try to explain myself better. Thanks very much!

---

<div class="post-metadata">

**Author:** ![richard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f0a364/32.png) [@richard](https://www.ruby-forum.com/u/richard)\
**Post date:** [November 26, 2006, 7:29pm UTC](https://www.ruby-forum.com/t/layouts-how-to-render-partials-from-multiple-controllers/77306/2 "2006-11-26T19:29:11Z")

</div>

in your view code just prefix the partial template with  
“controller\_name/partial\_name” like so:  
\<%= render :partial=\>‘search/search\_box’ %\>  
\<%= render :partial=\> ‘restraunts/menus’ %\>

hope thats what you were looking for.  
~Michael fairchild

---

<div class="post-metadata">

**Author:** ![richard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f0a364/32.png) [@richard](https://www.ruby-forum.com/u/richard)\
**Post date:** [November 26, 2006, 7:33pm UTC](https://www.ruby-forum.com/t/layouts-how-to-render-partials-from-multiple-controllers/77306/3 "2006-11-26T19:33:13Z")

</div>

Richard wrote:

> My second problem is that the login box functionality is part of an  
> account controller, the search box functionality is part of a search  
> controller etc So how can I render partials from all these different  
> controllers in this one layout?
> 
> I hope this makes sense. Please reply if any aspect is confusing and I  
> will try to explain myself better. Thanks very much!

The controller is up to you, as its really just personal preference. I  
usually use the users controller and a “welcome” action for such  
things. You can call partials from other controllers as such.

render :partial =\> ‘another\_controller/partial’

-b

---

<div class="post-metadata">

**Author:** ![richard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f0a364/32.png) [@richard](https://www.ruby-forum.com/u/richard)\
**Post date:** [November 26, 2006, 7:34pm UTC](https://www.ruby-forum.com/t/layouts-how-to-render-partials-from-multiple-controllers/77306/4 "2006-11-26T19:34:41Z")

</div>

Brandt wrote:

> Richard wrote:
> 
> > My second problem is that the login box functionality is part of an  
> > account controller, the search box functionality is part of a search  
> > controller etc So how can I render partials from all these different  
> > controllers in this one layout?
> > 
> > I hope this makes sense. Please reply if any aspect is confusing and I  
> > will try to explain myself better. Thanks very much!
> 
> The controller is up to you, as its really just personal preference. I  
> usually use the users controller and a “welcome” action for such  
> things. You can call partials from other controllers as such.
> 
> render :partial =\> ‘another\_controller/partial’
> 
> -b

Ah right. Thanks to both of you! This helped. I really didn’t realise it  
was as simple as that.

---

<div class="post-metadata">

**Author:** ![richard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f0a364/32.png) [@richard](https://www.ruby-forum.com/u/richard)\
**Post date:** [November 26, 2006, 7:51pm UTC](https://www.ruby-forum.com/t/layouts-how-to-render-partials-from-multiple-controllers/77306/5 "2006-11-26T19:51:15Z")

</div>

Richard wrote:

> Brandt wrote:

> > render :partial =\> ‘another\_controller/partial’

> Ah right. Thanks to both of you! This helped. I really didn’t realise it  
> was as simple as that.

Will the other controller control the other partial?

–  
Phlip  
[Redirecting...](http://www.greencheese.us/ZeekLand) ← NOT a blog!!!

---

<div class="post-metadata">

**Author:** ![richard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f0a364/32.png) [@richard](https://www.ruby-forum.com/u/richard)\
**Post date:** [November 26, 2006, 8:31pm UTC](https://www.ruby-forum.com/t/layouts-how-to-render-partials-from-multiple-controllers/77306/6 "2006-11-26T20:31:19Z")

</div>

Phlip wrote:

> > Ah right. Thanks to both of you! This helped. I really didn’t realise it  
> > was as simple as that.
> 
> Will the other controller control the other partial?

No, if your partial needs objects, they need to be supplied by the  
controller/action that is being performed. Components will achieve what  
I believe you are thinking of, but I really can’t comment on them as  
I’ve never used them myself.

-b

---

<div class="post-metadata">

**Author:** ![richard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f0a364/32.png) [@richard](https://www.ruby-forum.com/u/richard)\
**Post date:** [November 26, 2006, 11:11pm UTC](https://www.ruby-forum.com/t/layouts-how-to-render-partials-from-multiple-controllers/77306/7 "2006-11-26T23:11:51Z")

</div>

Brandt wrote:

> No, if your partial needs objects, they need to be supplied by the  
> controller/action that is being performed. Components will achieve what I  
> believe you are thinking of, but I really can’t comment on them as I’ve  
> never used them myself.

The moral of the Rails story is that a little bit of MVC is still  
much, much better than none!

–  
Phlip  
[http://c2.com/cgi/wiki?ZeekLand](http://c2.com/cgi/wiki?ZeekLand) ← NOT a blog!!
