Bar*_*red 7 ruby-on-rails file download carrierwave
我需要我的用户在他们的个人资料上上传ad pdf和txt等文档.我使用Carrierwave这样做,所以我有一个包含标题和网址的文档列表.
但是如何让其他用户下载这些文件?我是否必须使用宝石或是否有一些本土的我甚至不知道因为我对铁杆很新?
谢谢
编辑:
society.rb
class Society < ActiveRecord::Base
...
has_many :documents, :dependent => :destroy
end
Run Code Online (Sandbox Code Playgroud)
document.rb
class Document < ActiveRecord::Base
belongs_to :society
mount_uploader :url, DocumentUploader
end
Run Code Online (Sandbox Code Playgroud)
然后在视图中我要下载文件:
<% @society.documents.each do |doc| %>
<%= link_to "Download it", doc.url %> //url is the column name of the saved url
<% end %>
Run Code Online (Sandbox Code Playgroud)
Ant*_*rto 11
我不使用Carrierwave,但我猜它与Paperclip类似.
因此,你应该可以使用这样的东西
link_to 'Download file', user.file.url
Run Code Online (Sandbox Code Playgroud)
这假设您具有来自具有'file'carrierwave属性的Model的用户实例化对象.将其替换为您的属性名称.
Ben*_*ate 10
您可以使用此send_file方法.这看起来像:
class MyController
def download_file
@model = MyModel.find(params[:id])
send_file(@model.file.path,
:filename => @model.file.name,
:type => @model.file.content_type,
:disposition => 'attachment',
:url_based_filename => true)
end
end
Run Code Online (Sandbox Code Playgroud)
查看apidock链接以获取更多示例.