# Multiple Controllers in a page?

**URL:** <https://www.ruby-forum.com/t/multiple-controllers-in-a-page/75494>\
**Category:** Rails\
**Created:** [November 10, 2006, 10:07am UTC](https://www.ruby-forum.com/t/multiple-controllers-in-a-page/75494 "2006-11-10T10:07:41Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kev](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e9bcb4/32.png) [@Kev](https://www.ruby-forum.com/u/Kev)\
**Post date:** [November 10, 2006, 10:07am UTC](https://www.ruby-forum.com/t/multiple-controllers-in-a-page/75494/1 "2006-11-10T10:07:41Z")

</div>

Hi all,

I recently posted this on the ruby group instead of here ☹

but here is what I am attempting to do.

I am building a site, and I wish to populate the navigation dynamically  
from a MySQL db table, and the page content from a different table.

The idea being I can have the navigation on every page, and use  
different controllers to select different content depending on what is  
needed.

example:  
The Event page content comes from a page table where id = someValue  
but I also need some events (days out etc, presentations), they come  
from the events table.

I am confused how to put all the pieces together in one page.

Could anyone point me in the right direction?

\*\*  
At the moment I have a Site Controller, NavItem controller/model (with  
a partial \_nav\_item), Event controller/model and Page controller/model,  
all of which have the functions for list/show etc inside their  
respective controller, except the Site.

Since I posted I have created a partial for my nav\_items, but how do I  
then render my partial in another controller? I can’t understand how I  
could just use  
\<%= render :partial =\> “nav\_item”, object =\> @nav\_item %\>  
and rails know what “nav\_item” is.

Should I be looking at using a template?

If someone could provide some clarification on what I am doing wrong,  
or if I am trying to structure it wrong, that would be great 🙂

Cheers,  
Kev

---

<div class="post-metadata">

**Author:** ![Kev](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e9bcb4/32.png) [@Kev](https://www.ruby-forum.com/u/Kev)\
**Post date:** [November 10, 2006, 10:17am UTC](https://www.ruby-forum.com/t/multiple-controllers-in-a-page/75494/2 "2006-11-10T10:17:44Z")

</div>

1. About the Navigation:

add a before\_filter to your app controller that calls a method to  
build the data for your sidebar

before\_filter :build\_sidebar  
…  
private  
def build\_sidebar  
if controller\_name = “page”  
do something  
elseif controller\_name = “someothercontroller”  
do somethingelse  
end  
end

and then render your navigation partial in your layout  
(application.rhtml):  
\<%= reder :partial =\> :navigation %\>  
and in your \_navigation.rhtml you build your naviation based on the  
variables/data populated in the beforefilter.

2’) about the “different controllers in one page”  
i think you are thinking the wrong way. you have a controller for each  
Model, and want to put those controllers together. You dont need 1  
controller for each model.  
Instead, you should have a controller for each section/logic of your  
site, and in each of these controllers you access various Models to  
create your views etc.
