Render :partial => :url - is it possible?


#1

Hi, this is my first post to the list. I’m still learning the rails
framework, having done most of my previous web development in PHP. I’ve
done a bit of googling for the answer to this question, but maybe I’m
just lacking the right word to search for.

Several parts of my site, such as sidebar components, are
self-contained, modularized and re-used in several parts of the site.
The sidebar is part of the layout (I’m using the nested_layout plugin),
rather than the page view.

/app/layouts/layout_3_col.rhtml

<%= render :partial => @col_left_partial || 'col_left' %>

However this means I need to generate the data for the sidebar
components within each model, and not more-cleanly in a separate model
for the sidebar component itself.

What I am after is a system where I could create a separate model, view
and controller for each sidebar component and then call render :url =>
/sidebars/left_col/3

I’m assuming a similar approach would be used in AJAX, where you replace
a DIV with a HTML fragment generated on the server, but would want to
show the fragment as part of the original page without generating a
series of AJAX requests on page load.

Is this possible in rails, whats the technique called (so I can google
for it) or am I going about it the wrong way.

Thanks in advance.


James


#2

/app/layouts/layout_3_col.rhtml

and controller for each sidebar component and then call render :url =>
/sidebars/left_col/3

What you are looking for is in fact called “components” which are going
out of style (if they aren’t already) as they are inefficient.

If you have a sidebar that is shared among many areas of the site, yet
is
totally self contained, I would simply break MVC a little bit and make a
“MySideBarModel.find…” call followed by the rendering code to display
it.

Yes, it breaks MVC, but it’s a much simpler in a lot of ways.

Or if it’s really shared everywhere create a before_filter in your
application.rb that loads up the data needed for every request.

-philip