I have a link that downloads a dynamically generated file. The way I have it functioning is the following:
- I created a custom controller action on a resource (created a custom route in
routes.rb) - Clicking the link (non-ajax at this point) directs to this custom Controller action
- The file is generated, and the last line in the custom controller action is
send_datawhich streams the document to the user.
Update: It was recommended to include code. Here is the code in that custom controller action:
class MyController < ApplicationController
def custom_action_sending_pdf
pdf = InitMyPdf.new(@user)
send_data pdf.render, filename: "complete_report.pdf", type: "application/pdf"
end
end
The issue is that request.referrer for the next request will now reference this custom controller action, and I do not want it to do that. This is also reflected in the url.
- This specifically becomes an issue when later in my app I redirect back to
request.referrer. When therequest.referreris this custom controller action: it does not redirect to an actual page but instead just re-downloads the document all over again.
Instead I try this:
- I make the link to that custom action an ajax request via the
remote: trueoption
On one hand: the request.referrer now properly seems to not be referencing that controller action. However, now the file is not downloading!
I have done some looking around:
- This question does not have an answer that can be immediately applied.
- This question applies to downloading a static file which does not work for me because my file is dynamically generated, so I use
send_datato send the generated file to the user as opposed tosend_file.
Question: Ultimately I am trying to let a user download a dynamically generated file, all while NOT changing the page (keeping the request.referrer the same). Is this possible?
Update
I am aware of this question but the issue with it is that it's accepted answer is using send_file, and since my file is dynamically generated: send_file will not work for me here.
send_fileand I mention in my question thatsend_fileis not a usable solution for me.send_filesend_fileas described by the other questions is a good recommendation. I will look into that.