Rails使用块和集合呈现部分

时间:2016-01-19 09:55:33

标签: ruby-on-rails ruby

这类似于this question,但有一个集合:

<div class="panel-body">
    <%= render layout: 'today_items_list', locals: {items: @pos} do |po| %>
         <% @output_buffer = ActionView::OutputBuffer.new %>
        <%= link_to "##{po.id}", po %>
        <%= po.supplier.name %>
    <% end %>
  </div>

with partial / layout:

.tableless_cell.no_padding
  %h3.no_margin_vertical= title
  %ul.no_margin_vertical
    - for item in items
      %li= yield(item)    

这会按照您的预期呈现,但如果我省略了奇怪的&#39; @output_buffer = ActionView :: OutputBuffer.new&#39;,则不会清除缓冲区并以这种方式呈现列表:

<li>
  <a href="/purchase_orders/4833">#4833</a>Supplier name
</li>
<li>
  <a href="/purchase_orders/4833">#4833</a>Supplier name
  <a href="/purchase_orders/4835">#4835</a>Supplier name 2
</li>
<li>
  <a href="/purchase_orders/4833">#4833</a>Supplier name
  <a href="/purchase_orders/4835">#4835</a>Supplier name 2
  <a href="/purchase_orders/4840">#4840</a>Supplier name 3
</li>

永远不要在块调用之间清除缓冲区。我在这里缺少什么?

(乘坐Rails 3.2.22)

1 个答案:

答案 0 :(得分:0)

我对Rails 3.2没有多少经验;对于较新的版本,您可以通过collections to partials

var site, room, InstanceId;
var siteCheckRegex = new Regex("^Site:");

for(var i=0; i<lines.length; i++){

      if(siteCheckRegex.test(lines[i])){
        site = lines[i].replace("Site:","");
      }

      [...]
}

你可以read more about collections etc (I presume for Rails 4+) here