user1034016
user1034016

Reputation: 21

Rails: Send dynamically generated file already available to the view as a download

I have a controller in a Rails application that generates a set of CSV files and provides them to the view. I would like to add links allowing a user to download those files in the same view. I've looked through quite a number of download methods, but all seem to require additional controller actions. Is it possible to provide links to dynamically generate files within the same controller action that they were generated?

e.g.:

#Other view content here, page specific header stuff, formatted csv output, etc
@csv_files.each do |csv_file|
  #spit out a download link to the CSV object in memory
end

Upvotes: 1

Views: 223

Answers (1)

Chris Heald
Chris Heald

Reputation: 62648

Short answer: no. When the user clicks on a link, they're making another request to the server. That request will have to be handled as a download request.

If you're worried about generation time of these files, consider storing them in Memcached or similar, so that your download action can just pull them from there, rather than having to regenerate them.

Upvotes: 2

Related Questions