You're right, it's because of render
. When you're setting headers
, and then finally call render
, you're building up an HTTP response to be sent to the browser. You can't do this twice from one HTTP request, which is why you get the ActionController::DoubleRenderError
.
One way to send multiple .csv
files at once is to combine them into a single .zip
file and send that instead of the raw .csv
files. There is an excellent, very easy to use rubyzip
gem you can use to build your .zip
file.
Instead of calling render inside stream_csv
, you can simply return the FasterCSV
instance itself or it's String output. From the calling method you can build up a .zip file (I'm not including code for that here since I'd just be copying and pasting from the README
at the gem above).
Finally, instead of setting so many headers to force the download, take a look at Rails send_file
method