Better render_if_exists

Hello,

I’m building a little polymorphic page (I mean that you use the same
page to deal with objects coming from different models, for instance
users and products) and I need to render some partial templates if they
exist, and get no error if they don’t (for instance I’ve got a partial
template called _optional_fields_user which I’d like to load, but
_optional_fields_product doesn’t exist and I’d like to use the same code
to load both.

So I build a little helper :

def render_if_exists toRender
begin
render toRender
rescue ActionView::ActionViewError
end
end

and I call it :

<%=render_if_exists :partial =>“optional_fields_#{@element.className}”%>

Everything works fine but I think it’s ugly to do it this way, would
anyone have anything better?

Thanks