我正在使用mongoid-paperclip gem上传文件。我能够显示我上传的图像。但是如何显示pdfs / videos。
请帮我解决问题。
这是图像模型。
Class Image
has_mongoid_attached_file :logo
validates_attachment :logo, :content_type => [ "application/pdf"]
validates_attachment_content_type :logo, :content_type => ["image/jpg", "image/jpeg", "image/png"]
field :logo
end
查看文件:
<% @images.each do |image| %>
<%= image_tag(image.logo.url(:original)) %>
<% end %>
提前致谢。
答案 0 :(得分:1)
替换
validates_attachment :logo, :content_type => [ "application/pdf"]
validates_attachment_content_type :logo, :content_type => ["image/jpg", "image/jpeg", "image/png"]
与
validates_attachment_content_type :logo, :content_type => ["image/jpg", "image/jpeg", "image/png", "application/pdf"]
当您验证相同的logo
字段时,无需单独验证。
我假设您已安装GhostScript
(如果使用Mac,则为brew install gs
)。这是使用Paperclip
进行pdf上传的要求。如果没有,请先安装它并重启rails服务器。
在您的视图中,添加以下代码:
<% @images.each do |image| %>
<% if image.logo.image_content_type == 'application/pdf' %>
<iframe src=<%= image.logo.url(:original) %> frameborder="0"></iframe>
<% else %>
<%= image_tag(image.logo.url(:original)) %>
<% end %>
<% end %>
iframe
是一个内联框架机制,用于在当前HTML中嵌入另一个文档。
上面的代码使用环境进行测试:Ruby 2.1.0,Rails 4.0.2,Mac OS Mavericks,Paperclip 3.5.3
修改强>
通过ffmpeg查看用于向Paperclip添加视频处理的paperclip-ffmpeg gem。确保您的计算机上安装了FFMPEG
。
答案 1 :(得分:0)
将此添加到您的模型
has_attached_file :logo,
:url => "/assets/products/:id/:style/:basename.:extension",
:path => ":rails_root/public/assets/products/:id/:style/:basename.:extension"
然后在视野中
<p>
<%= link_to 'My PDF', @variable.logo.url %>
</p>