Checking if a paperclip attachment exists within a view.

4.2k views Asked by At

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 %>
1

There are 1 answers

1
Teddy Valente On

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