Render can only be called once per request. It's results are stored in the response and returned to the browser. So there is no way this will work. We have to either return one mondo object, or change this into multiple requests. As intriguing as it is, in this post we will leave aside all the wonderful solutions that could be done with javascript to retrieve multiple requests.

To Zip or Not To Zip

The obvious answer for returning the One response is to zip up all the individual spreadsheets and return that. To get the contents of each spreadsheet we will use render_to_string. To create the zip file, lets start by storing each result in a file, compressing them, and returning the zip file:

So this works. But ... I don't know about you, but using the temp directory feels pretty hacky to me. There has to be another way! And, of course, it turns out there is. (There always is!) Enter rubyzip.

The Sol

We can avoid going to disk entirely. The trick is to use rubyzip's write_buffer to create the zip file in memory, and to rewind the buffer before sending it to the response: