# Better render\_if\_exists

**URL:** <https://www.ruby-forum.com/t/better-render-if-exists/114566>\
**Category:** Rails\
**Created:** [September 15, 2007, 2:57pm UTC](https://www.ruby-forum.com/t/better-render-if-exists/114566 "2007-09-15T14:57:32Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![esox\_ch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/4da419/32.png) [@esox\_ch](https://www.ruby-forum.com/u/esox_ch)\
**Post date:** [September 15, 2007, 2:57pm UTC](https://www.ruby-forum.com/t/better-render-if-exists/114566/1 "2007-09-15T14:57:32Z")

</div>

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
