ruby-on-railsfiledownloadruby-on-rails-5link-to

How do I create a download link that doesn't blink?


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

Solution

  • 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 add data: { turbolinks: false } to opt-out of Turbolinks for this particular link. The code after changes should look like this:

    <%= link_to image_tag("cc_icon.png"), scenario_download_cc_path(subscription.scenario), data: { turbolinks: false } %>
    

    Your controller action looks good and needs no changes.