Ruby on Rails - Multiple Controllers in a page?

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?

On 11/9/06, Kev [email protected] 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
(http://wiki.rubyonrails.com/rails/pages/Components)

Hope that helps,

Glenn

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 :]
[email protected]

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
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.
[email protected]

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!

Don’t use components!

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.

Giles B. wrote:

Don’t use components!

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.

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

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

-carl

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs