Folks, (This might be a completely newbie question, so please excuse the ignorance.) If I wanted to make a new controller/view that combined information from some other views that I already had built, what would be the best way to do that? In other words (as I''m struggling to explain this properly :-)... Imagine I''ve created models/controllers/views for: a shopping list, a calendar and a calculator. Now imagine I want to make a single page showing a view from all three, probably rendered into DIVs on the page (I''d rather not use IFRAMEs or anything like that). I suppose what I was hoping for is something like: <% render "calendar/listsmall/3" %> where that URL still results in the same "plumbing" between models/controllers/views. But since the URL magic seems to happen as part of the web request, I''m not sure how this would work. And pointers would be *much* appreciated :) Thanks, Kevin
Kevin, I''ve used what Rails calls a "Partial" to accomplish this kind of behavior. An example of a partial might be _shopping_list.rhtml (note the leading "_"). Then in your view (wherever you want the shopping list to appear) you would use: <%= render_partial "shopping_list" %> You can also specify the directory structure in the partial to render so that you can share partials across views or even across modules. Note that you still have to set up the data in the controller properly in each place that you will be rendering the partial (e.g. You would need "@items = Items.find_all" so that the @items array is availabe to the _shopping_list.rhtml partial). You can find more on these at http://rails.rubyonrails.com under "ActionView::Partials" Duane Johnson (canadaduane) On Wed, 02 Feb 2005 10:07:35 -0600, Kevin McConnell <kevin.mcconnell-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Folks, > > (This might be a completely newbie question, so please excuse the > ignorance.) > > If I wanted to make a new controller/view that combined information from > some other views that I already had built, what would be the best way to > do that? > > In other words (as I''m struggling to explain this properly :-)... > Imagine I''ve created models/controllers/views for: a shopping list, a > calendar and a calculator. Now imagine I want to make a single page > showing a view from all three, probably rendered into DIVs on the page > (I''d rather not use IFRAMEs or anything like that). > > I suppose what I was hoping for is something like: > > <% render "calendar/listsmall/3" %> > > where that URL still results in the same "plumbing" between > models/controllers/views. But since the URL magic seems to happen as > part of the web request, I''m not sure how this would work. > > And pointers would be *much* appreciated :) > > Thanks, > Kevin > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Duane Johnson wrote:> I''ve used what Rails calls a "Partial" to accomplish this kind of > behavior....> Note that you still have to set up the data in the controller properly > in each place that you will be rendering the partialPartials sound like the right idea, thanks. I''ll read up on those. I''d hoped to be able to have it as an extra action/view on the existing class, to be tidy. But hey you can''t have everything :) (And actually I guess I could probably stick the setup code & the render_partial call into a class method of the original controller, which would acheive pretty much the same thing.) Cheers, Kevin
Perhaps we need an extension to the partials system that lets a partial do some processing? Maybe if the _partial is defined on the corresponding controller, it gets called with all the request data set? (I realize, this is not easy in the current architecture, since only the active controller is instantiated, but it should be possible to transparently do this on demand in the partials system) Of course, a controller knows which partials its view intends to render so in the current system, you could define the code for them in the application controller and then do something like: def method_that_uses_partials do_something_with @params prepare_shopping_cart prepare_todo_list end and then have your ''shared/_todo.rhtml'' and ''shared/_shopping_cart.rhtml'' referenced in the method_that_uses_partials.rhtml file. If these elements appear in your layouts for a bunch of pages (rather than in each action) then you can put a before_filter in your application controller to condition on the current layout and load the ones that are used all over. Just a thought. I kind of like the second way better than bloating the core with this, especially considering that my first suggestion violates MVC a little bit and could be confusing. Brian On Wed, 02 Feb 2005 16:32:24 -0600, Kevin McConnell <kevin.mcconnell-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Duane Johnson wrote: > > > I''ve used what Rails calls a "Partial" to accomplish this kind of > > behavior. > ... > > Note that you still have to set up the data in the controller properly > > in each place that you will be rendering the partial > > Partials sound like the right idea, thanks. I''ll read up on those. > > I''d hoped to be able to have it as an extra action/view on the existing > class, to be tidy. But hey you can''t have everything :) > > (And actually I guess I could probably stick the setup code & the > render_partial call into a class method of the original controller, > which would acheive pretty much the same thing.) > > Cheers, > Kevin > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- The years ahead pick up their dark bags. They move closer. There''s a slight rise in the silence then nothing. -- (If you''re receiving this in response to mail sent to bluczkie-OM76b2Iv3yLQjUSlxSEPGw@public.gmane.org, don''t be concerned This is my new address, but mail will be forwarded here indefinitely)