I have a folder in my app directory named \"uploads\" where users can upload files and download files. I don\'t want the uploads folder to be in the public directory becaus
For anyone having this problem, I solved it. Pass
'data-no-turbolink' => true
into the link_to helper to stop Turbolinks from messing with the download.
https://github.com/rails/turbolinks/issues/182
But if you are using a form with turbooboost = true
, instead of link_to, or even with a link_to you can do it like this:
Inside your controller, and inside your action put:
def download
respond_to do |format|
format.html do
data = "Hello World!"
filename = "Your_filename.docx"
send_data(data, type: 'application/docx', filename: filename)
end
format.js { render js: "window.location.href = '#{controller_download_path(params)}';" }
end
end
Replace controller_download_path
with a path to your download
action,
and place in your routes both post
and get
for the same path:
post '/download' => 'your_controller#download', as: :controller_download
get '/download' => 'your_controller#download', as: :controller_download