Forum: Ruby on Rails checking existence of a partial

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
94f7106912be0ff6b67775f6b12950af?d=identicon&s=25 Mark Silva (Guest)
on 2006-04-04 19:02
(Received via mailing list)
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
Aa4cb81d5f9428ddea2329602fdb875e?d=identicon&s=25 Mark Silva (Guest)
on 2006-04-04 19:38
(Received via mailing list)
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
31b6446447a4da5157bb67cc4c6afc64?d=identicon&s=25 John Kodis (Guest)
on 2006-04-05 13:32
(Received via mailing list)
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 ""
%>
B45eab4f18aa1bb2a44d6e657531a642?d=identicon&s=25 Alain Ravet (aravet)
on 2006-04-12 12:48
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
31b6446447a4da5157bb67cc4c6afc64?d=identicon&s=25 John Kodis (Guest)
on 2006-04-15 00:56
(Received via mailing list)
On Wed, Apr 12, 2006 at 12:48:22PM +0200, Alain Ravet 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.
This topic is locked and can not be replied to.