4

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_data which 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 the request.referrer is 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: true option

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_data to send the generated file to the user as opposed to send_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.

5
  • So that I can be more specific, whoever voted that this question be closed: please explain where the ambiguity is in my question that warrants it being closed. I will try to be more clear. Commented Feb 23, 2018 at 15:54
  • 1
    Possible duplicate of How to trigger download with Rails send_data from AJAX post Commented Feb 23, 2018 at 16:27
  • @Rob the accepted answer in that question is using send_file and I mention in my question that send_file is not a usable solution for me. Commented Feb 23, 2018 at 16:36
  • 1
    I think you wouldn't get so many close votes if you included your code in the question. I think the question is clear enough, but I think it's kind of a kneejerk reaction of answerers to vote to close a question whenn it has a bunch of text describibng code and no code block. Annyway, just to give my quick 2 cents, I would consider 2 options: 1, manually change request.referrer in the controller action, if that's possible. 2, create a tempfile with the dynamically generated content and send that over with send_file Commented Feb 23, 2018 at 19:00
  • @maxpleaner thanks for your input. I added what applicable code I could. I didn't add it before because I wasn't really getting an "error" due to my code. I understand why it is behaving as it is. I just do not know how to go about getting the desired behavior. Your suggestion of creating a file and using send_file as described by the other questions is a good recommendation. I will look into that. Commented Feb 23, 2018 at 19:14

1 Answer 1

3

This works. use button_to

Example:

<%= button_to(<custom_action_sending_pdf_path>, method: :get, class: 'btn btn-primary') do %>
  <i class="fa fa-print" aria-hidden="true"></i></i> Print PDF
<% end %>

Let that button go to that custom action which downloads the pdf (same code as in original question):

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

What happens now is when that button is clicked:

  • The pdf downloads and pops up adobe (at least it does this in safari)
  • The url of the page does not change to the custom action which prints the pdf. So for the next request: request.referrer will not be that custom action for the pdf, which is the desired behavior here.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.