Perish

博客园 首页 联系 订阅 管理

每天学一点,天天都是进步!

 content_for在次模板中使用,比如index.html.erb或者其他,对应的yield在主模板中使用,对于index.html.erb来说的application.html.erb中使用。

在index.html.erb中有如下代码:

<% content_for(:list) do %>

  <ol>

     <% for i in 1..5 %>

          <li>I'm <%= i %> !</li>

      <% end %>

  </ol>

<% end %>

然后在application.html.erb中的body中用yield来显示index中的content_for中的内容如下:

<%= yield :list %>

 这个时候一定要指定是list这样才会显示,如果只是<%= yield %> 这样是不会去显示content_for(:list)里的内容的!

 实际上是yield通过传递的不同参数, 控制不同的代码块!

posted on 2012-09-12 16:37  ---小青年---  阅读(219)  评论(0编辑  收藏  举报