我试图做一个简单的链接,点击时启动与特定资源关联的文档的下载。我有一个资源模型,在该模型中有一个名为“文档”的列。点击链接时,我可以成功查看内联文档,但我更愿意下载它。我一直在阅读有关内容类型和send_file的内容,但是我一直无法完全将它组合在一起以实现功能。从Carrierwave文档附件的链接下载文件
这里是我想我需要使用的链接代码:
<%= link_to 'Download File', :action => "download_file" %>
这将引发一个错误:
的ActiveRecord :: RecordNotFound在ResourcesController#秀 可能找不到ID资源= download_file
当我更改链接到这一点,它会打开浏览器中的文件:
<%= link_to 'Open file', resource.document_url, :target => "_blank" %>
在我ResourcesController我已经定义了这个方法:
def download_file
@resource = Resource.find(params[:id])
send_file(@resource.file.path,
:filename => @resource.file.name,
:type => @resource.file.content_type,
:disposition => 'attachment',
:url_based_filename => true)
end
我在routes.rb中设置了这样的路线:
resources :resources do
get 'resources', :on => :collection
end
所以基于这个错误,它似乎是我的download_file方法在ResourcesController中无法确定与该文档相关联的资源记录的ID。
我运行: 的Rails 3.2.11 Carrierwave 0.8.0 的Ruby 1.9.3-P194
我希望得到一些洞察到这一点。我搜查了一些文章,但无法找到简单的教程。谢谢。
非常感谢。这一切都创造了奇迹。 Rails 4 - CarrierWave。 – 2015-01-27 03:17:06