Sairam Suresh
Sairam Suresh

Reputation: 75

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

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 ?

Upvotes: 1

Views: 2451

Answers (2)

Nitin
Nitin

Reputation: 7366

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.

Upvotes: 0

jabbrwcky
jabbrwcky

Reputation: 638

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...)

Upvotes: 1

Related Questions