Rails PaperClip Attachments, knowing if there is a thumbnail image?

I use a Rails 3 clip and let users upload attachments to the attachment model.

If the file is an image, the application generates previews of the images. If the file is missing, it only downloads the file (without previewing the image).

Now I would like to display a list of all attachments in the database. Therefore, I use attachment.attachment (: large) and works great for image attachments, but errors (obviously) for attachments without an image.

What is a good way to check if there is an image attachment or not? If not, I would like to display a standard static image. Any suggestions? thank

+5
source share
4 answers

This is what I did from my point of view:

<% if !(@attachment.attachment.content_type =~ /^image/).nil? %>
<%= image_tag @attachment.attachment.url(:small) %>
<%end%>

This assumes that your model is an attachment, and my file, I am the so-called attachment.

So you can do something like:

<% if !(@attachment.attachment.content_type =~ /^image/).nil? %>
<%= image_tag @attachment.attachment.url(:small) %>
<%else%>
<%= image_tag "/path/to/image/default.png" %>
<%end%>
+6
source

Check out attachment.attachment.attachment_content_type

For example, it could be: "image / jpeg"

+3
source

, attachment_content_type . , paperclip . , - "image/jpeg".

0

All Articles