2016-11-14 46 views
0

我正在使用rails_admin gem和Paperclip。我的模型是這樣的:下載鏈接在Rails_中的回形針附件

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

回答

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 
2

你只需要做的。

<%= 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 
+0

如果這一邏輯依然走在我的'提交/ index.html.erb'? –

+0

我的意思是,即使我使用'rails-admin' gem? –

+0

我不能在管理員視圖中包含它,因爲我沒有視圖,所以如何在初始化程序中的rails_admin.rb下添加該鏈接? –