解决rails4.0中send_file文件下载两次的问题

来源:互联网 发布:天津医科大学考研知乎 编辑:程序博客网 时间:2024/05/18 01:39

遇到的问题

之前在开发文件下载的功能时,我遇到了一个很奇怪的问题,点击下载链接,在chrome console中会出现两次请求,第一次返回200,下载的数据缓存在chrome的cache中,第二次返回304,直接从本地获取到下载的数据。查看服务器log,的确返回了两次数据。经过很长时间的定位与搜索,终于找到了问题的原因——rails4.0中引入了turbolink技术——当网页改变时,不会刷新整个页面而是直接替换掉html中的head和body——也就是说切换页面时不会看到浏览器的刷新操作。

Turbolinks makes following links in your web application faster. Instead of letting the browser recompile the JavaScript and CSS between each page change, it keeps the current page instance alive and replaces only the body (or parts of) and the title in the head. Think CGI vs persistent process. ——[ GitHub ]


解决方法

在views中的haml文件中禁用turbolink。

link_to "下载", download_app_path(id), class: "btn btn-sm btn-primary", 'data-no-turbolink' => true
0 0