How to perform both Render and Redirect for a given Action in rails

2.3k views Asked by At

i m working on an app in rails that requires the following :

  1. Clicking on a button should first download a pdf file (render part) and
  2. Then redirect to a show page (redirect_to part)

I 've searched the web and found that it is not possible to render and redirect at the same time for a given action , is there any ways around this issue ?

2

There are 2 answers

1
Nitin On

send_data using render, so you can't do anything after it in your controller.

To do this you have to use javascript:

  1. Create a iframe on button click. (That will hold your download process).

  2. Redirect the page on the iframe's close event.

You can also use a background job for download action but I haven't used it yet.

0
jabbrwcky On

You could send the user to a result page and add a meta tag to redirect the user to a download after x seconds.

<meta http-equiv="refresh" content="5; url=http://path_to/your/download.zip"/>

This is the approach you see at a lot of sites e.g. sourceforge.net (Your download will start after x seconds...)