我希望在请求的开头呈现一些文本,然后在结尾呈现一些其他文本。
这就是我正在尝试做的事情
class DevelopmentController < ApplicationController
skip_before_filter :require_login
before_action { not_found! unless Rails.env.development? }
def pry
render text: 'Go to your server, a pry session awaits!', stream: true
require 'pry'
binding.pry
render text: 'Pry session complete.'
end
end
它让我很不爽,因为我调用了render两次:AbstractController::DoubleRenderError
我也试着直接写到流中,它的工作原理是在结束时呈现,但在请求结束之前它不会呈现文本(即不是流):
def pry
response.stream.write 'Go to your server, a pry session awaits!'
require 'pry'
binding.pry
response.stream.write 'Pry Complete'
end
我想可能需要刷新流,但显然没有flush方法,所以IDK。
发布于 2015-03-28 04:46:36
你需要包含include ActionController::Live
才能流媒体,它在sleep
下工作得很好,但由于某些原因,它不能在byebug下工作,但你需要确保连接在你完成时是关闭的,否则它将保持打开。下面是一个例子
class MyController < ActionController::Base
include ActionController::Live
def stream
100.times {
response.stream.write "<p>#{Time.now}</p>"
sleep 1
}
ensure
response.stream.close
end
end
该模块是documented here,由于某种原因,当我添加示例中提供的头文件时,它会提示下载而不是流。
https://stackoverflow.com/questions/23250807
复制相似问题