views:

1633

answers:

1

I have a rails app that allows a user to download a generated CSV file. After the file is downloaded, I'd like to redirect the user to a new URL or action. Is there a trick to do a redirect after a send_data?

I want to do something like this (which doesn't work):

send_data(output,:type => content_type,:filename => "myfile.csv")
redirect_to :controller => 'my_controller', :action => 'download_done'
+3  A: 

send_data is a render, so you can't do anything after it in a controller method.

The ways around it all involve Javascript. Here's one way I hacked together and never really liked:

  • user hits submit
  • small window pops up and connects to the controller to download the file
  • main window redirects to the next page

Eventually I turned file generation into a background job. Then the user went to a jobs page (that didn't need a subsequent redirect) to get the files.

Sarah Mei
thanks- this worked well for my needs!
daustin777
This technique worked well until a user with IE6 and very strict browser security settings came along and it stopped working. I ended up using an <iframe> as the target of the download action which seems to work in all of my tested environments.
daustin777