3

我是 Rails 的新手,并且是 Ruby 的新手。我已经建立了一个基于 ruby​​ 1.9.2 和 rails 3.1 的 rails 应用程序

我遇到了一个问题,可能是因为我对 Rails 中的流式实现的理解。在我看来,在 rails 渲染模板内容之前,它必须完成评估它的过程。

例如,假设我有一个如下所示的 cgi 脚本

#!/bin/sh


cat <<END
Content-Type: text/html

END

for i in {1..10}
do
 echo $i
 sleep 1
done

这将在浏览器中按顺序显示从 1 到 10 的数字,因为它们是由脚本呈现的,而不是一次全部显示。这是我想在 rails 视图中模拟的行为。

假设我有以下观点。

<h1> hi </h1>
<%
sleep 5
%>
<h2> bye </h2>

它会等到 sleep 5 完成后再显示任何内容。

使用 curl -i 观察来自 Web 服务器的响应,我立即按预期使用 Transfer-Encoding: 分块获得响应标头。但是,在睡眠完成之前,它不会渲染正文内容。

我需要做什么才能让它以与我在上面粘贴的简单 cgi 脚本相同的方式呈现正文内容?具体来说,首先是<h1> hi </h1> 5 秒后<h2> bye </h2>

4

1 回答 1

0

Take a look at the documentation for ActionController::Streaming. To wit:

Streaming can be added to a given template easily, all you need to do is to pass the :stream option.

class PostsController
  def index
    @posts = Post.scope
    render :stream => true
  end
end

You'll want to read the rest of the docs for the details, of course.

于 2011-10-21T13:40:35.307 回答