Ruby on rails 从路径下载文件的链接
我在我的机器上有一个文件的路径,我想为它设置下载链接。以下是我正在尝试的: 在我的模型中:Ruby on rails 从路径下载文件的链接,ruby-on-rails,Ruby On Rails,我在我的机器上有一个文件的路径,我想为它设置下载链接。以下是我正在尝试的: 在我的模型中: class Exam < ActiveRecord::Base attr_accessible :data, :full_path has_attached_file :image, :path => :full_path end 我的看法是: <%= link_to "Download", download_exam_path(@exam) %> 在你
class Exam < ActiveRecord::Base
attr_accessible :data, :full_path
has_attached_file :image,
:path => :full_path
end
我的看法是:
<%= link_to "Download", download_exam_path(@exam) %>
在你看来,
@exam
似乎是nil
。这可能是因为它在下载
操作中才被实例化,但您正试图在索引
或显示
操作中使用它,而这些操作尚未构建
如果是这种情况,那么您只需要将此(或类似的内容-我不确定params[:id]
是否可以在其他操作中使用,就像在下载
操作中一样)添加到控制器中导致错误的操作中:
@exam = Exam.find(params[:id])
更新
根据更新显示的完整错误指向tests\u controller.rb
的第83行,您在评论中确认为:
send_file @exam.image.path, :x_sendfile => true
我会打开Rails控制台(Rails c
),然后输入:
@exam = Exam.find(params[:id])
然后,我将开始检查@exam
的哪个部分是nil
,方法是尝试以下两行:
@exam.image
@exam.image.path
您可以根据该测试找出问题。不确定我是否遵循。你是对的,我正试图在我的表演中使用它。但是根据我的错误报告,错误来自我的下载操作,它已经有了那块代码。我遗漏了什么吗?我想我只是误解了
不能将nil转换为字符串的地方。你能添加一份你在问题中遇到的完整错误的副本吗?好的-我已经更新了我的答案,并介绍了调试该问题的后续步骤。错误现在更改为无法将回形针::附件转换为字符串啊哈!如果我将send_file@exam.image.path,:x_sendfile=>true
更改为send_file@exam.full_路径,:x_sendfile=>true
,则代码可以工作。我想我只需要绳子就行了。感谢您的帮助+1并接受。Issend_file@exam.image.path,:x_sendfile=>true
tests\u controller.rb的第83行
?只要问一下,您正在使用回形针
?我正在使用回形针,是的。
send_file @exam.image.path, :x_sendfile => true
@exam = Exam.find(params[:id])