# Checking existence of a partial

**URL:** <https://www.ruby-forum.com/t/checking-existence-of-a-partial/56834>\
**Category:** Rails\
**Created:** [April 4, 2006, 7:02pm UTC](https://www.ruby-forum.com/t/checking-existence-of-a-partial/56834 "2006-04-04T19:02:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mark\_Silva](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ecccb3/32.png) [@Mark\_Silva](https://www.ruby-forum.com/u/Mark_Silva)\
**Post date:** [April 4, 2006, 7:02pm UTC](https://www.ruby-forum.com/t/checking-existence-of-a-partial/56834/1 "2006-04-04T19:02:07Z")

</div>

Is there a simple way to check for the existence of a partial before  
displaying it?

I have some code that creates a dynamic partial depending on the city a  
particular object is located in. There are some cities that do NOT have  
partials defined for them, but I still want the overall page to  
display. Is there a way to either chefck for existence before  
displaying (enclosed in an if statement?) or to tell the partial to  
skip if it cannot find the file?

\<%= render :partial =\> ‘listing/partials/additional\_’ + @city %\>

Thanks,  
Mark

---

<div class="post-metadata">

**Author:** ![Mark\_Silva](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ecccb3/32.png) [@Mark\_Silva](https://www.ruby-forum.com/u/Mark_Silva)\
**Post date:** [April 4, 2006, 7:38pm UTC](https://www.ruby-forum.com/t/checking-existence-of-a-partial/56834/2 "2006-04-04T19:38:39Z")

</div>

Answering my own question:

File.exist? “#{RAILS\_ROOT}/app/views/listing/partials/_additional_” +  
@city + “.rhtml”

If there is a better/cleaner way let me know.

Thanks,  
Mark

---

<div class="post-metadata">

**Author:** ![Mark\_Silva](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ecccb3/32.png) [@Mark\_Silva](https://www.ruby-forum.com/u/Mark_Silva)\
**Post date:** [April 5, 2006, 1:32pm UTC](https://www.ruby-forum.com/t/checking-existence-of-a-partial/56834/3 "2006-04-05T13:32:31Z")

</div>

Don’t worry about whether the partial exists or not, just assume it  
does, and rescue from the error if this fails. Something like…

\<%= render(:partial =\> ‘listing/partials/additional\_’ + @city) rescue “”  
%\>

---

<div class="post-metadata">

**Author:** ![Mark\_Silva](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ecccb3/32.png) [@Mark\_Silva](https://www.ruby-forum.com/u/Mark_Silva)\
**Post date:** [April 12, 2006, 12:48pm UTC](https://www.ruby-forum.com/t/checking-existence-of-a-partial/56834/4 "2006-04-12T12:48:22Z")

</div>

John Kodis wrote:

```
> Don't worry .. just assume it does, and rescue from the error if 

```

this fails.  
\> \<%= render(:partial =\> ‘listing/partials/additional\_’ + @city)  
rescue “” %\>

In Java, capturing exceptions slows down the code you’re watching.  
What’s the situation in Ruby? Is it free to use ‘rescue’?

Alain

---

<div class="post-metadata">

**Author:** ![Mark\_Silva](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ecccb3/32.png) [@Mark\_Silva](https://www.ruby-forum.com/u/Mark_Silva)\
**Post date:** [April 15, 2006, 12:56am UTC](https://www.ruby-forum.com/t/checking-existence-of-a-partial/56834/5 "2006-04-15T00:56:00Z")

</div>

On Wed, Apr 12, 2006 at 12:48:22PM +0200, Alain R. wrote:

> John Kodis wrote:
> 
> ```
> > Don't worry .. just assume it does, and rescue from the error if 
> 
> ```
> 
> this fails.  
> \> \<%= render(:partial =\> ‘listing/partials/additional\_’ + @city)  
> rescue “” %\>
> 
> In Java, capturing exceptions slows down the code you’re watching.  
> What’s the situation in Ruby? Is it free to use ‘rescue’?

It’s not free, but I suspect that rescue is quicker than testing for  
the existance of a file and dealing with its presence or absence.

– John Kodis.
