Ruby on Rails - Multiple Controllers in a page?

K

Kev

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

Glenn Gillen

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
 
G

Gustav Paul

Kev said:
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...?

<div id="page_content">
<%= Page.find_by_controller_and_action("event", "list").page_content %>
</div>
<div id="event_list">
<%= Event.find:)all, :eek:rder => 'created_at desc', :limit => 20).each
do |event| %>
<!-- do something with 'event', eg. -->
<div id="event_<%=event.id%>">Title: <%=event.title%> </div>
<% end %>
</div>

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 :]
(e-mail address removed)

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 Paul
(e-mail address removed)
 
K

Kev

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!

Gustav said:
Kev said:
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...?

<div id="page_content">
<%= Page.find_by_controller_and_action("event", "list").page_content %>
</div>
<div id="event_list">
<%= Event.find:)all, :eek:rder => 'created_at desc', :limit => 20).each
do |event| %>
<!-- do something with 'event', eg. -->
<div id="event_<%=event.id%>">Title: <%=event.title%> </div>
<% end %>
</div>

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 :]
(e-mail address removed)

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 Paul
(e-mail address removed)
 
G

Giles Bowkett

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.

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!

Gustav said:
Kev said:
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...?

<div id="page_content">
<%= Page.find_by_controller_and_action("event", "list").page_content %>
</div>
<div id="event_list">
<%= Event.find:)all, :eek:rder => 'created_at desc', :limit => 20).each
do |event| %>
<!-- do something with 'event', eg. -->
<div id="event_<%=event.id%>">Title: <%=event.title%> </div>
<% end %>
</div>

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 :]
(e-mail address removed)

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 Paul
(e-mail address removed)
 
D

David Vallner

--------------enigC4EDD33153E924B2BD2B249F
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

Giles said:

"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 Vallner


--------------enigC4EDD33153E924B2BD2B249F
Content-Type: application/pgp-signature; name="signature.asc"
Content-Description: OpenPGP digital signature
Content-Disposition: attachment; filename="signature.asc"

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (MingW32)

iD8DBQFFVd9Ey6MhrS8astoRAoKsAJ4+Gnh/hFFZytaUzxCCGHfCFfvyDgCfZimY
+a7zwke6zet64z29nrVXDpQ=
=YuUk
-----END PGP SIGNATURE-----

--------------enigC4EDD33153E924B2BD2B249F--
 
C

Carl Lerche

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

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

-carl
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top