Forum: Ruby on Rails Checking if Image file exists before displaying it?

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.
C4b929d5f7755cd93f362f424c6c3852?d=identicon&s=25 Corey Murphy (rubylicious)
on 2008-11-24 18:51
I want a page to display an image for a record if the image file
actually exists. If the file does not exist (newly created record) I
want the placeholder image to display. I've tried using File.exist? as
my conditional but it fails to find the files that do exist.  Any ideas
how to easily handle this at the view code level?


View Code:

<% for e in @media_elements %>
     <% if File.exist?(e.filename) %>
         <%= link_to(image_tag(e.filename.to_s) -%>
     <% else %>
         <%= link_to(image_tag("placeholder_image.jpg") -%>
     <% end %>
<% end %>
C4b929d5f7755cd93f362f424c6c3852?d=identicon&s=25 Corey Murphy (rubylicious)
on 2008-11-24 19:01
Nevermind.
It was a path issue, just provided some more info on the full path to
the image and the File.exist? method worked just fine.  Sorry for
wasting the space.
This topic is locked and can not be replied to.