From: Gustav Paul Date: 2006-11-10T02:03:57+09:00 Subject: Re: Ruby on Rails - Multiple Controllers in a page? 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 :] rubyonrails-talk@googlegroups.com 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 gustav@rails.co.za