Rails how do I - export data with send_data then redirect_to a new page?

后端 未结 2 1828
长发绾君心
长发绾君心 2020-11-29 09:32

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

相关标签:
2条回答
  • 2020-11-29 10:02

    In my case, I have a download link in page 1 and after the successfull download, i just want to redirect them to page 1 ( same page which has the download link ). If you have a same case like mine, please do the below one.

    Just add the target="_blank" in your download button/link. This is only for send_data render method. If you use send_file, you have to handle this in js.

    It redirects to the separate page and pops up a download file save option. Once you saved, it closes the new tab and takes them to the page which you clicked the download button.

    0 讨论(0)
  • 2020-11-29 10:11

    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.

    0 讨论(0)
提交回复
热议问题