Checking if a paperclip attachment exists within a view.
Asked Answered
P

1

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 %>
Practical answered 8/5, 2016 at 21:7 Comment(2)
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)Galactic
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 wellPractical
S
8

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 %>
Steviestevy answered 22/6, 2016 at 9:41 Comment(1)
I believe it should be .exists? and not .exist? So <% if @post.image.exists? %> for the example above.Efferent

© 2022 - 2024 — McMap. All rights reserved.