Ruby on rails rails 3.2创建下载链接NoMethodError
我正在尝试在我的rails应用程序中下载文件。我在我的应用程序中看到并使用了。但是,当我尝试下载时,出现以下错误:Ruby on rails rails 3.2创建下载链接NoMethodError,ruby-on-rails,ruby-on-rails-3.2,download,nomethoderror,Ruby On Rails,Ruby On Rails 3.2,Download,Nomethoderror,我正在尝试在我的rails应用程序中下载文件。我在我的应用程序中看到并使用了。但是,当我尝试下载时,出现以下错误: NoMethodError in ConfsController#download undefined method `xml' for nil:NilClass app/controllers/confs_controller.rb:61:in `download' 这是confs_控制器: def download send_file @conf.xml.path, :t
NoMethodError in ConfsController#download
undefined method `xml' for nil:NilClass
app/controllers/confs_controller.rb:61:in `download'
这是confs_控制器:
def download
send_file @conf.xml.path, :type => @conf.xml_content_type, :filename => @conf.permalink
end
xml是我的文件。它只是一个与示例中的文档类似的名称。现在,我知道出现此错误是因为xml为零。但是为什么以及如何修复它呢?问题不在于xml是
nil
。这是因为@conf
是nil
。因此,它无法找到nil
类的xml
请确保定义了
@conf
对象。什么是@conf
?在你的例子中它是nil,所以你得到了nil:NilClass的undefined method
xml`实际上我是这么想的,但是如果你看看我给出的链接,它以同样的方式使用@thing。为什么我的坏了?我应该使用self或类似的东西吗?您的文件在sendfile中的位置您必须将该路径作为第一个参数send_file'/path/to.jpeg',:type=>'image/jpeg',:disposition=>'inline'
,以这种方式路径取决于@conf.id:rails_root/downloads/:attachment/:id/:basename.:extension“
所以,我不能这样做,我把
放在show.html.erb和@conf中,那里不是零。如何将show中的@conf分配给controller中的@conf?我添加了@conf=conf.find(conf_-params)
,conf_-params是这样的:params.require(:conf).permit(:id,:xml,其他属性),如果params[:conf]
现在它说,没有id就找不到conf
,但是请求参数:{“id”=>“6”}
我将@conf=conf.find(conf_params)
更改为@conf=conf.find(params[:id])
,并将:id=>@conf.id
添加到链接到的show.html.erb
中,谢谢