下载Rails_admin中Paperclip附件的链接

时间:2016-11-14 20:57:42

标签: ruby ruby-on-rails-4 paperclip attachment rails-admin

我正在使用rails_admin gem和Paperclip。我的模型看起来像这样:

class Product < ActiveRecord::Base
  has_attached_file :asset,
    :styles => {
      :thumb => "100x100#",
      :small  => "150x150>",
      :medium => "200x200" }
  validates_attachment_content_type :asset, :content_type => /\Aimage\/.*\Z/
end

如何在index操作中添加下载链接?那么,在admin/products表中的每个条目都有一个下载链接?我阅读了文档,但他们似乎没有指定任何这些功能。

[编辑]

在我在此处路由的主索引操作:/products我以前做过:

<%= link_to "Download", product.asset.url(:original, false) %>

2 个答案:

答案 0 :(得分:3)

你只需要做。

<%= link_to "Download", product.asset(:original) %>

<%= link_to "Download", product.asset.url(:original) %>

他们都做同样的事情。

如果您想更改他们下载的图片版本,只需将:original更改为:medium:small:thumb

对于Rails管理员,请执行以下操作:

config.model "Product" do
  list do
   ....
    field :download do
      formatted_value do
        bindings[:view].tag(:a, href: bindings[:object].assets(:original)) << "Download"
       end
     end
   end
   ...
 end

答案 1 :(得分:1)

[解决]

提交模式:

class Submission < ActiveRecord::Base

  # Image attachment and validations
  has_attached_file :file,
    :url => "/files/:class/:attachment/:id/:style/:basename.:extension",
    :path => ":rails_root/public/files/:class/:attachment/:id/:style/:basename.:extension"

  validates_attachment_content_type :file, :content_type => 'application/pdf'

end