问题出在下面的代码(“routes.rb”)
post 'download_csr' do
file_name = File.dirname(__FILE__) + '/cert.csr'
File.open(file_name, 'w') do |f|
f.write params[:csr]
end
send_file file_name, :disposition => 'attachment'
erb :load_certificate, :locals => { :csr => 'cert.csr' }
end
流程是这样的:用户点击表单上的按钮,然后写入文件并下载到本地系统,然后转到另一个页面(即“load_certificate.erb”)。
然而,真正发生的是文件下载后,它并没有转到下一页。但是如果我注释掉“send_file”,它会转到下一页。那么如何解决这个问题呢?非常感谢!
还有一件事:在转到下一页之前确保文件确实已下载会很好(因为当 send_file 弹出一个窗口时,用户可以选择“取消”)。一些超时机制可能在这里不起作用。
最佳答案
@Soup 的链接很适合阅读。关于 Sinatra,send_file
calls halt
,结束请求(在本例中通过发送状态和文件)。 Nothing will be processed after a halt
.
关于ruby-on-rails - ruby 西纳特拉 : redirect page not working?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20339513/