2014-09-24 102 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,但没有使用它,虽然