5

Check whether a Paperclip attachment exists: I'm trying to see if the attachment has selected a file/image then something will happen like this new view to appear. So after i click browse select an Image I want something to happen!! Thanks!

Nothing happens when i do this code!

home view:

<% if @post.image.present? %>
            <%= render 'posts' %>
            <% end %>
Michael Lombardo
  • 457
  • 4
  • 20
  • Is there an alternative you'd like to see if there is no image uploaded? (so that something happens in the event that there is no image present) – gonzalo2000 May 08 '16 at 21:35
  • i would actually prefer not to see an alt image. Thats another problem as it outputs missing before i try to upload a photo. I want to remove the "missing" message as well – Michael Lombardo May 09 '16 at 02:25

1 Answers1

7

You should use the exists? or file? methods.
exists?: will check if the file actually exist (slow)
file?: will only check if there is a value in db (faster).

<% if @post.image.exists? %>
    <%= render 'posts' %>
<% end %>
Kyle Heironimus
  • 7,741
  • 7
  • 39
  • 51
  • 4
    I believe it should be .exists? and not .exist? So `<% if @post.image.exists? %>` for the example above. – Bryan Jun 16 '17 at 21:04