2014-09-24 90 views
0

在我看來,我有 -導軌 - nilclass在控制器動作

<%= link_to 'remove pdf', controller: 'chap_comments', action: 'remove_file', id: comment.id %> 

在我的控制器我有 - !

def remove_file 
    @chap_comment.remove_chap_comment_pdf! 
    @chap_comment.save 
end 

...我越來越undefined method remove_chap_comment_pdf」爲零:NilClass` - 爲什麼班級不被識別?

同樣,以下 -

<%= link_to 'remove pdf', remove_file_chap_comment_path(:id), method: :delete %> 

...得到了同樣的錯誤。

回答

3
def remove_file 
    @chap_comment = ChapComment.find(params[:id) 
    @chap_comment.remove_chap_comment_pdf! 
    @chap_comment.save 
end 

您還沒有找到chap評論或分配它,所以@chap_comment是零。

0

您傳遞link_toid而不是用它來查找記錄這就是爲什麼你收到此error.use它: -

@chap_comment = ChapComment.find(params[:id) 
    ##do anything with this @chap_comment 

在服務器日誌(控制檯)看看,你可以看到是這樣的: -

Processing by ChapsController#remove_file as HTML 
    Parameters: {"id"=>"9"} 

這意味着 - 你擁有的是ID,但沒有使用它,雖然