首先,什么是流媒体?你为什么要使用它?
流式传输是自上而下(由外向内)呈现页面的替代方法。默认渲染行为是由内向外的。必须在控制器中启用流式传输:
class MyController
def action
render stream: true # Streaming enabled
end
end
根据文档 http://api.rubyonrails.org/classes/ActionController/Streaming.html:
对于轻量级操作来说,流媒体可能被认为是过度杀伤力
喜欢新建或编辑。流媒体的真正好处在于昂贵
例如,对数据库进行大量查询的操作。
那么,如果您不使用流媒体,还有区别吗?
Yes.
区别在于模板可以定义多个内容块通过致电content_for
多次。这样做将连接块并将其传递给布局:
# layout.html.erb
<div class="heading"><%= yield :surprise %></div>
<div class="body">
<p><%= yield %></p>
<p>But it's not very interesting...</p>
</div>
# template.html.erb
<%= content_for :surprise, "Hello" %>
I've got your content!
<%= content_for :surprise, ", World!" %>
# Generated HTML
<div class="heading">Hello, World!</div>
<div class="body">
<p>I've got your content!</p>
<p>But it's not very interesting...</p>
</div>
Since provide
不继续搜索提供的模板,仅传递给第一个的块provide
调用将被发送到模板:
# layout.html.erb
<div class="heading"><%= yield :title %></div>
# template.html.erb
<%= provide :title, "Foo" %>
<%= provide :title, "bar" %>
# Generated HTML
<div class="heading">Foo</div>