Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails PaperClip Attachments, knowing if there's a image thumbnail?

I'm using Rails 3 paperclip and allow users to upload attachments to the attachment model.

If the file is an image, the app generates image previews. If the file is not, it only uploads the file (no image previews).

Now I would like to display a list of all the attachments in the DB. So I use attachment.attachment(:large) and that works fine for image attachments, but errors (obviously) for non-image attachments.

What's a good way to check if it's an image attachment or not? If not, I'd like to display a standard static image. Any suggestions? thanks

like image 246
AnApprentice Avatar asked Jan 26 '11 04:01

AnApprentice


2 Answers

This is what I did in my view:

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

This assumes that your model is attachment, and my file, I so called attachment.

So you could 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%>
like image 143
Travis Pessetto Avatar answered Nov 14 '22 23:11

Travis Pessetto


Check attachment.attachment.attachment_content_type

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

like image 28
TK-421 Avatar answered Nov 15 '22 00:11

TK-421