Question

Instead of displaying the xml file rendered by the index.api.rsb file in my browser, i want to download it. To me this sounds very simple, but I cant find a solution.

I tried the following in the controller-method:

def split
   if params[:export] == "yes"
       send_file *here comes the path to xml view*, :filename => "filename", :type => :xml
   end
   respond_to ...
end

The result is a MissingFile exception...

Thanks in advance

Was it helpful?

Solution

Note that :disposition for send_file defaults to 'attachment', so that shouldn't be a problem.

If you have a MissingFile exception, that means the path is incorrect. send_file expects the path to an actual file, not a view that needs to be rendered.

For your case, render_to_string might be what you need. Refer to this related question. It renders the view and returns a string instead of setting the response body.

def split
  if params[:export] == "yes"
    send_data(render_to_string path_to_view, filename: "object.xml", type: :xml)
  end
end

OTHER TIPS

To force it to download it, add :disposition => attachment to your send_file method.

Source: Force a link to download an MP3 rather than play it?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top