Rails 4, asset pipeline causes user downloadable files to be downloaded twice

前端 未结 2 793
执笔经年
执笔经年 2021-01-14 03:21

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

相关标签:
2条回答
  • 2021-01-14 03:47

    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

    0 讨论(0)
  • 2021-01-14 03:47

    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
    
    0 讨论(0)
提交回复
热议问题