# Ruby on Rails - Multiple Controllers in a page?

**URL:** <https://www.ruby-forum.com/t/ruby-on-rails-multiple-controllers-in-a-page/75394>\
**Category:** Ruby\
**Created:** [November 9, 2006, 5:21pm UTC](https://www.ruby-forum.com/t/ruby-on-rails-multiple-controllers-in-a-page/75394 "2006-11-09T17:21:21Z")\
**Posts on this page:** 7\
**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 9, 2006, 5:21pm UTC](https://www.ruby-forum.com/t/ruby-on-rails-multiple-controllers-in-a-page/75394/1 "2006-11-09T17:21:21Z")

</div>

Hi all,

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, 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?

---

<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 9, 2006, 5:33pm UTC](https://www.ruby-forum.com/t/ruby-on-rails-multiple-controllers-in-a-page/75394/2 "2006-11-09T17:33:23Z")

</div>

On 11/9/06, Kev [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi all,
> 
> 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.

Options that immediately come to mind:  
a) Have the controllers load the data from the relevant sections into  
instance variables which are then passed to the views for processing.  
Menu stuff being handled by a layout, controlled specific by the view  
for the specific action you are undertaking

b) Make the menu a seperate component  
([Peak Obsession](http://wiki.rubyonrails.com/rails/pages/Components))

Hope that helps,

Glenn

---

<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 9, 2006, 6:05pm UTC](https://www.ruby-forum.com/t/ruby-on-rails-multiple-controllers-in-a-page/75394/3 "2006-11-09T18:05:29Z")

</div>

Kev wrote:

> Hi all,

Hey

> 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  
> It sounds like this is the kind of thing you’d want in your layout, or,  
> if you’re using more than one layout, the navigation should be in a  
> partial that you could then render in each of the particular layouts…  
> , and use  
> different controllers to select different content depending on what is  
> needed.

I’m not quite sure what you mean, but ‘content’ usually refers to the  
views that get rendered by the actions in your controllers…So  
‘depending on what’s needed’ usually means you have seperate views for  
your different actions.

> example:  
> The Event page content comes from a page table where id = someValue  
> but I also need some events, they come from the events table.

This sounds like you’ve a view that should look as follows…?

 \<%= Page.find\_by\_controller\_and\_action("event", "list").page\_content %\>

 \<%= Event.find(:all, :order =\> 'created\_at desc', :limit =\> 20).each do |event| %\> 
Title: \<%=event.title%\> 
 \<% end %\>

Assuming you’re keeping the page content in a table called pages with  
atleast four columns: id, controller and action (which together specify  
which action in which controller should have the text rendered in the  
view for) and page\_content, which contains the actual page content. I’m  
assuming since you want to display a list of events, you’re rendering  
the ‘list’ action in an event\_controller (I’m guessing here :] ). The  
previous post suggested getting this kind of data in your controller and  
pass it to this view as instance variables, this is better practice.

The second part of the code will get the last 20 events and display  
their titles.

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

I hope the above code helps. I’m almost 100% sure I don’t understand  
what you want fully, if you’d care to elaborate a little bit more I’d be  
happy to help you as best I can.

> Could anyone point me in the right direction?

The right direction is undoubtedly on the Rails Mailing List :]  
removed\_email\_address@domain.invalid

The previous post suggested components,  
Some people believe they are evil … It’s your call though :] If you’re  
using components, you most likely want to be using partials instead, but  
that’s a 100% IMHO!  
[http://glu.ttono.us/articles/2006/08/30/guide-things-you-shouldnt-be-doing-in-rails](http://glu.ttono.us/articles/2006/08/30/guide-things-you-shouldnt-be-doing-in-rails)  
The basic idea is that using a component is like calling a new action,  
with rendered view etc, so unless you want to include an entire other  
view that already lives elsewhere in your app, use a partial  
instead…if of course that is the case, then using a component is  
perfect.

Anyway, Cheery-O  
Gustav P.  
removed\_email\_address@domain.invalid

---

<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 9, 2006, 6:31pm UTC](https://www.ruby-forum.com/t/ruby-on-rails-multiple-controllers-in-a-page/75394/4 "2006-11-09T18:31:35Z")

</div>

Thanks guys,

I shall look into components, and when I have more time (I’m currently  
at work!) digest Gustav’s post more fully, although from my shaky  
outline you seem to understand very well what I wish to do!

---

<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, 2:17am UTC](https://www.ruby-forum.com/t/ruby-on-rails-multiple-controllers-in-a-page/75394/5 "2006-11-10T02:17:04Z")

</div>

Don’t use components!

[http://glu.ttono.us/articles/2006/08/30/guide-things-you-shouldnt-be-doing-in-rails](http://glu.ttono.us/articles/2006/08/30/guide-things-you-shouldnt-be-doing-in-rails)

Also this really should be on the Rails list.

---

<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 11, 2006, 3:33pm UTC](https://www.ruby-forum.com/t/ruby-on-rails-multiple-controllers-in-a-page/75394/6 "2006-11-11T15:33:28Z")

</div>

Giles B. wrote:

> Don’t use components!
> 
> [http://glu.ttono.us/articles/2006/08/30/guide-things-you-shouldnt-be-doing-in-rails](http://glu.ttono.us/articles/2006/08/30/guide-things-you-shouldnt-be-doing-in-rails)

“For The PHP Trolls” on the end sent me into a giggle fit.

Also more on-topic, noone said you can’t have code outside a controller.  
Instead of looking into Rails hackery, just abstract the navigation  
logic into a separate component that only calls ActiveRecord classes for  
the model, and renders a partial with them when called? You only really  
need it to be a controller if it’s doing… well… control flow.

David V.

---

<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 12, 2006, 3:29am UTC](https://www.ruby-forum.com/t/ruby-on-rails-multiple-controllers-in-a-page/75394/7 "2006-11-12T03:29:09Z")

</div>

Yeah, use components… if 15 seconds per request is acceptable.

(I inherited that code, I didn’t develop it myself!)

-carl
