3

In the controller I have:

send_data(data,  filename: "payment_file")

In the view I have:

<%= link_to 'Download File', bank_file_download_path(@bank_file_job),
    data: {no_turbolink: true}, class: "btn btn-primary btn-lg btn-block" %>

But it always shows the content of the file inline. Could someone please help me?

Lluís
  • 1,267
  • 1
  • 14
  • 36
coffeecoder
  • 514
  • 7
  • 7

3 Answers3

9

I figured this out. As already pointed out in another answer turbolinks need to be disabled. But the suggested solution in that answer didn't work because the syntax for disabling it on a specific page has changed from turbolinks 5.0 onwards. It should now be like this.

<%= link_to 'Download File', file_download_path , data: {turbolinks: false}   %>
Community
  • 1
  • 1
coffeecoder
  • 514
  • 7
  • 7
0

According to the docs, if you want it to be download, then you need to set the :disposition to download

eg

send_data data, filename: "payment_file", disposition: 'download' 

http://apidock.com/rails/ActionController/DataStreaming/send_data

Taryn East
  • 27,486
  • 9
  • 86
  • 108
  • Thanks for the answer @Taryn. But "disposition: attachment" is the default so you don't usually need to specify it. If you say "disposition: inline" then it will show the content in the browser. But if you say nothing about disposition then it defaults to attachment which means download should work. So that was not the problem in this case – coffeecoder Nov 13 '16 at 20:15
  • Great to hear you found the real solution. It's often worth checking any default by being explicit... sometimes you find that something else somewhere magic has changed your defaults. – Taryn East Nov 13 '16 at 21:13
0

Dont forget to add the format to the file name, it should end in .pdf for example so the browser knows what to do