简体   繁体   中英

Rails render partial with block and collection

This is similar to this question but with a collection:

<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)    

This renders as you expect but if I omit the weird '@output_buffer = ActionView::OutputBuffer.new', the buffer is not cleared and the list is rendered this way:

<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>

Never clearing the buffer between block invocation. What am I missing here?

(Riding on Rails 3.2.22)

I don't have much experience with Rails 3.2; with newer versions, you're able to pass collections to partials :

<%= render @pos, layout: 'today_items_list' %>

#app/views/pos/_today_items_list.html.erb
.tableless_cell.no_padding
   %h3.no_margin_vertical= title
     %ul.no_margin_vertical
        <%= yield %>

#app/views/post/_po.html.erb
<%= link_to "##{po.id}", po %>
<%= po.supplier.name %>

You can read more about collections etc (I presume for Rails 4+) here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM