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
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
Mark Silva wrote:> 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
> _______________________________________________
> Rails mailing list
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>
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 "" %> On Tue, Apr 04, 2006 at 10:36:10AM -0700, Mark Silva wrote:> 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 > > > Mark Silva wrote: > >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 > >_______________________________________________ > >Rails mailing list > >Rails@lists.rubyonrails.org > >http://lists.rubyonrails.org/mailman/listinfo/rails > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails
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
--
Posted via http://www.ruby-forum.com/.
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.