2013-07-19 168 views
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 
+0

''exams_controller.rb'的'send_file @ exam.image.path,:x_sendfile => true'行83? –

+0

是的,的確如此。 – DashControl

+0

只是問,你在用'paperclip'嗎? –

回答

1

這聽起來像@exam是在視圖nil。這可能是因爲它在download操作之前沒有實例化 - 但是您正嘗試在indexshow操作中使用它,但操作尚未建立。

如果是這種情況,那麼你只需要添加這個(或者類似的東西 - 我不確定params[:id]是否可用於其他動作,因爲它在download動作中)在您的控制器是造成錯誤:

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

更新

基於可見完整的錯誤指向到行exams_controller.rb 83,你在爲成爲您的評論證實了更新:

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

您可能能夠根據該測試找出問題。

+0

不知道我是否關注。你是正確的,我正試圖在我的節目中使用它。但根據我的錯誤報告,錯誤來自我的下載操作,該操作已經有了該代碼塊。我錯過了什麼嗎? – DashControl

+0

我想我只是誤解了'不能將nil轉換成String'的地方。你能否在你的問題中添加完整錯誤的副本? –

+0

是的,它已更新。 – DashControl