On Tue, Aug 09, 2005 at 11:26:46AM +1200, Matt Powell
wrote:> Hi all,
>
> Does anyone know of a(n elegant, extensible) way to render a different
> partial based on the subclass of the object in question? I mean, you
> could do it with miles of is_a? statements, but that''s not exactly
very
> clean, right?
>
> The problem, as I see it, is that you can''t call render() from the
> model, which is where all the polymorphism happens. If there was some
> way to get this to work, it would all be fine.
Try something like this out, see if it works:
<%% @results.each do |result| %>
<hr />
<%%# Try to render a partial based upon the class name %>
<%%# but if that does not work, render a generic one %>
<%% begin %>
<%%= render_partial Inflector.underscore(result.class), result %>
<%% rescue ActionView::ActionViewError %>
<pre>No parital available for <%%= result.class %>, using
generic </pre>
<%%= render_partial "generic", result %>
<%% end %>
<%% end %>
Create a partial _classname.rhtml in the appropriate view directory, and
create a default _generic.rhtml file to fall back to.
enjoy,
-jeremy
--
======================================================================= Jeremy
Hinegardner
jeremy-eg9WJcVQRd1EMKyauTCisw@public.gmane.org