I would like the user to be able to download a file from a method I set up in a controller. Further, I don’t want the URL to change in my browser when the user downloads the file. I have this link set up
<%= link_to image_tag("cc_icon.png"), scenario_download_cc_path(subscription.scenario), target: '_blank' %>
The problem is, there is a screen blink as a new tab is spawned to generate the download. This looks visually unappealing. I have seen other sites where you click the link and something starts downloading without a blink. How do I do that?
Edit: Here is the function invoked by the link
def download_cc
scenario = Scenario.find(params[:scenario_id])
send_data scenario.cc_data, filename: "#{scenario.title}.imscc", type: 'application/zip', :disposition => 'attachment'
end
I did some local testing and my hypothesis is that Turbolinks is messing things up. I recommend that you remove
target: '_blank'
from the link and adddata: { turbolinks: false }
to opt-out of Turbolinks for this particular link. The code after changes should look like this:Your controller action looks good and needs no changes.