Rails PaperClip 附件,知道是否有图像缩略图? [英] Rails PaperClip Attachments, knowing if there's a image thumbnail?

查看:47
本文介绍了Rails PaperClip 附件,知道是否有图像缩略图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Rails 3 回形针并允许用户将附件上传到附件模型.

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).

现在我想显示数据库中所有附件的列表.所以我使用了attachment.attachment(:large),它适用于图像附件,但对于非图像附件来说是错误的(显然).

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

推荐答案

这就是我认为的:

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

所以你可以这样做:

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

这篇关于Rails PaperClip 附件,知道是否有图像缩略图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆