0

我的机器上有一个文件的路径,我想为其设置下载链接。这是我正在尝试的:

在我的模型中:

class Exam < ActiveRecord::Base
   attr_accessible :data, :full_path
   has_attached_file :image,
      :path => :full_path
end

我的控制器看起来像这样:

def download
   @exam = Exam.find(params[:id])
   send_file @exam.image.path, :x_sendfile => true 
end

而我的观点:

<%= link_to "Download", download_exam_path(@exam) %>

现在,当我单击下载时,我收到此错误:can't convert nil into String 我知道一个事实,:full_path其中包含我的文件的正确路径。我怎样才能解决这个问题?

完整错误:

 TypeError in ExamsController#download

can't convert nil into String

Rails.root: /Users/Ryan45/Programming/rails_projects/oldV_rails_project
Application Trace | Framework Trace | Full Trace

app/controllers/exams_controller.rb:83:in `download'

Request

Parameters:

{"id"=>"392"}

Show session dump

Show env dump
Response

Headers:

None
4

1 回答 1

1

这听起来像是@examnil你看来。这可能是因为它直到在download动作内部才被实例化 - 但是您试图在indexorshow动作中使用它,它还没有被构建。

如果是这种情况,那么您只需将它(或类似的东西 - 我不确定是否params[:id]可以在其他操作中使用,因为它在download操作中)到您的控制器中导致的操作错误:

@exam = Exam.find(params[:id])

更新

根据显示完整错误指向第 83 行的更新exams_controller.rb,您在评论中确认为:

send_file @exam.image.path, :x_sendfile => true

我会打开 Rails 控制台 ( rails c),然后输入:

@exam = Exam.find(params[:id])

然后我会通过尝试这两行来开始检查哪个部分@exam是:nil

  1. @exam.image
  2. @exam.image.path

您可能能够根据该测试找出问题所在。

于 2013-07-19T19:25:19.443 回答