Rails 如何 - 使用 send_data 导出数据然后重定向到新页面? [英] Rails how do I - export data with send_data then redirect_to a new page?
问题描述
我有一个 rails 应用程序,允许用户下载生成的 CSV 文件.下载文件后,我想将用户重定向到新的 URL 或操作.在 send_data 之后是否有重定向的技巧?
I have a rails app that allows a user to download a generated CSV file. After the file is downloaded, I'd like to redirect the user to a new URL or action. Is there a trick to do a redirect after a send_data?
我想做这样的事情(不起作用):
I want to do something like this (which doesn't work):
send_data(output,:type => content_type,:filename => "myfile.csv")
redirect_to :controller => 'my_controller', :action => 'download_done'
推荐答案
send_data
是一个渲染,所以你不能在控制器方法中做任何事.
send_data
is a render, so you can't do anything after it in a controller method.
解决它的方法都涉及 Javascript.这是我一起黑客攻击但从未真正喜欢过的一种方式:
The ways around it all involve Javascript. Here's one way I hacked together and never really liked:
- 用户点击提交
- 弹出小窗口,连接控制器下载文件
- 主窗口重定向到下一页
最终我将文件生成变成了后台作业.然后用户转到作业页面(不需要后续重定向)以获取文件.
Eventually I turned file generation into a background job. Then the user went to a jobs page (that didn't need a subsequent redirect) to get the files.
这篇关于Rails 如何 - 使用 send_data 导出数据然后重定向到新页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!