0

I am trying to download files in my rails app. I saw How to Create Download Link and used in my app. But when I tried to download, I get this error:

NoMethodError in ConfsController#download
undefined method `xml' for nil:NilClass
app/controllers/confs_controller.rb:61:in `download'

This is confs_controller:

def download
  send_file @conf.xml.path, :type => @conf.xml_content_type, :filename => @conf.permalink
end

xml is my file. Its just a name like document in example. Now, I understand that this error appears because xml is nil. But why and how can I fix it?

Community
  • 1
  • 1
kalahari
  • 895
  • 5
  • 15
  • 34
  • what is `@conf`? here it is nil in your case so you got `undefined method `xml' for nil:NilClass` – Rajarshi Das Sep 09 '13 at 08:04
  • Actually I thought that, but if you look at the link I gave, it uses the @thing in the same way. Why mine is not working? Should I use self or something like that? – kalahari Sep 09 '13 at 08:08
  • where is your file located in sendfile you have to put that path as a first parameters `send_file '/path/to.jpeg', :type => 'image/jpeg', :disposition => 'inline'` in this way – Rajarshi Das Sep 09 '13 at 08:14
  • Path is depend on @conf.id `:path => ":rails_root/downloads/:attachment/:id/:basename.:extension"` So, I can't do that – kalahari Sep 09 '13 at 08:26

1 Answers1

2

The problem is not xml is nil. It is because @conf is nil. Hence it is not able to find xml for nil class.

Please make sure you have @conf object defined.

Marek Lipka
  • 50,622
  • 7
  • 87
  • 91
Suresh G
  • 36
  • 2
  • I put `<%= link_to 'Download', download_conf_path(@conf) %>` to show.html.erb and @conf in there is not nil. How can I assign @conf in show to @conf in controller? – kalahari Sep 09 '13 at 08:12
  • I added `@conf = Conf.find(conf_params)` and conf_params is this: `params.require(:conf).permit( :id, :xml, other attributes) if params[:conf]` Now it says, `Couldn't find Conf without an ID` but `Request Parameters: {"id"=>"6"}` – kalahari Sep 09 '13 at 08:28
  • I changed `@conf = Conf.find(conf_params)` to `@conf = Conf.find(params[:id])` and added `:id => @conf.id` to `link_to` at `show.html.erb` and it works now, thanks – kalahari Sep 09 '13 at 09:37