Checking existence of a partial


#1

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


#2

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


#3

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 “”
%>


#4

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


#5

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.