Reputation:
I’m using Rails 5. I have this link that a user will click on to download a file …
<%= link_to "#{scenario_file.title}", scenario_file_path(scenario_file) %>
It links off to this controller method …
def show
@scenario_file = ScenarioFile.find(params[:id])
send_data @scenario_file.file_data, filename: "#{@scenario_file.title}", type: @scenario_file.mime_type, :disposition => 'attachment'
end
My problem is, when someone clicks on the link, the browser URL changes to the link (obviously). However, is there any way I can rewrite the above link so that the file will download but my browser’s URL won’t change?
Upvotes: 1
Views: 923
Reputation: 5214
Try to add target="_blank"
to link:
<%= link_to "#{scenario_file.title}", scenario_file_path(scenario_file), target: '_blank' %>
Too you can use html5 download attribute. The download attribute is triggering a force download.
<%= link_to scenario_file.title, scenario_file_path(scenario_file), download: scenario_file.title %>
Upvotes: 2