Ruby on Rails send_file 在我刷新页面之前不起作用? [英] Ruby on Rails send_file doesn't work until i refresh the page?

查看:18
本文介绍了Ruby on Rails send_file 在我刷新页面之前不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Rails 服务器上工作,我可以从中下载本地存储的电影和动漫等.这是一种工作,但当我单击下载链接时,我必须刷新页面才能真正开始下载.

I am working on a Rails server which I can download my locally stored movies and anime etc from. This is kind of working but when I click the download link I have to refresh the page in order for the download to actually start.

这是处理下载的控制器:

This is the controller that handles the download:

class DownloadController < ApplicationController
  def index
    @title = params[:title]
    @name = params[:name]
    @path = '/media/sf_Anime,_VN,_LN/Watching, not watched yet/'+@title+'/'+@name
    send_file( @path )
  end
end

这是链接到该控制器的链接:

and this is the link that links to that controller:

<% @episodes.each do |x| %>
<p> <%= x %><%= link_to " Download", 
    {controller: 'download', action: 'index', title: @title, name: x } %> </p>
<% end %>

我今天做了一些测试,发现如果我尝试发送较小的文件(文本或图像),下载链接会立即生效.我还注意到下载链接实际上也适用于电影,但需要 20-30 秒才能开始下载.您知道什么会导致这种延迟吗?

edit: I did some testing today and noticed that the download links work instantly if i try to send a smaller file (text or image). I also noticed that the download links actually works for the movies aswell but it takes 20-30 seconds for the download to start. Do you have any idea of what would cause this delay?

推荐答案

你在使用 turbolinks 吗?Turbolinks 显然不能很好地与 send_file (https://github.com/rails/turbolinks/问题/182).尝试在 link_to 帮助器中添加data: { no-turbolink: true }"(或'data-no-turbolink'=>true"),例如:

Are you using turbolinks? Turbolinks apparently doesn't play nicely with send_file (https://github.com/rails/turbolinks/issues/182). Try adding "data: { no-turbolink: true }" (or "'data-no-turbolink'=>true") in the link_to helper, e.g.:

<%= link_to "Downloadable File", downloadable_file, data: { no-turbolink: true } %>

另见:Rails 4,资产管道导致用户可下载文件被下载两次rails 不会send_data 作为文件Ruby onRails send_file,控制器动作中的代码运行两次

这篇关于Ruby on Rails send_file 在我刷新页面之前不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆