Rails PaperClip附件,知道是否有图像缩略图?

AnA*_*ice 5 ruby-on-rails paperclip ruby-on-rails-3

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

如果文件是图像,则应用会生成图像预览.如果文件不是,则仅上传文件(无图像预览).

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

什么是检查它是否是图像附件的好方法?如果没有,我想显示一个标准的静态图像.有什么建议?谢谢

Tra*_*tto 6

这就是我在观点中所做的:

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

这假设你的模型是附件,我的文件,我称之为附件.

所以你可以这样做:

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


Hei*_*kki 0

也许你可以使用default_url选项?如果真实的缩略图不存在,则会显示该信息。

http://www.suffix.be/blog/default-image-paperclp