我正在使用Carrierwave将图像,文档和视频上传到我的s3存储桶。到目前为止,上传图像和文档是可以的。
我想在视图中做的是确定文件类型,然后显示图像(目前可以执行此操作),或者提供文档的图像,单击该图像将为用户下载/打开该文件的副本。 。
因此,我认为要渲染图像,我会这样做
<% document.each do |doc| %>
<%= link_to image_tag(doc.media_url(:thumb)) %>
<% end %>
但是我要怎么说
<% document.each do |doc| %>
<% if doc.file_type == ['jpg', 'jpeg', 'png']
<%= link_to image_tag(doc.media_url(:thumb)) %>
<% else %>
<%= link_to doc.media.path %> # This link downloading the file
<% end %>
<% end %>
我猜(让其他人猜出您应该在问题中提供的内容不是一件好事),您有一个模型文档,而您的上传者是媒体,像这样:
class Document < ActiveRecord::Base
mount_uploader :media, MediaUploader
end
在这种情况下,对于每个文档,您都将获得扩展名(document.media.file.extension.downcase
)并将其与“ jpg”,“ jpeg”,“ png”进行比较
<% document.each do |doc| %>
<% if ['jpg', 'jpeg', 'png'].include?(document.media.file.extension.downcase) %>
<%= link_to image_tag(doc.media_url(:thumb)) %>
<% else %>
<%= link_to doc.media.path %> # This link downloading the file
<% end %>
<% end %>
如果需要,Carrierwave可以通过以下方式为您提供内容类型:
document.media.content_type # this returns image/png for a png file ...
编辑:
我认为更好的方法是像这样检查它(它更干净):
<% document.each do |doc| %>
<% if document.media.content_type =~ /image/ %>
<%= link_to image_tag(doc.media_url(:thumb)) %>
<% else %>
<%= link_to doc.media.path %> # This link downloading the file
<% end %>
<% end %>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句