想知道什么是在视图中将模型上的循环拆分到TWO表上的最佳方法。看起来很简单。
<div>
<table>
<tr><th>Refreshments and Exhibits</th></tr>
<% @exhibitor.each do |exhibitor| %>
<tr>
<td><%= exhibitor.name %></td>
</tr>
<% end %>
</table>
</div>
<div>
<table>
<tr><th>Refreshments and Exhibits</th></tr>
<% @exhibitor.each do |exhibitor| %>
<tr>
<td><%= exhibitor.name %></td>
</tr>
<% end %>
</table>
</div>
这两次显示相同的表格。我想循环遍历@exhibitor以填充第一个表中的td,限制为15.然后继续循环遍历第二个表的Exhibitor.name的其余部分。
答案 0 :(得分:3)
如果您想要15个表格,请执行此操作
<% @exhibitors.each_slice(15) do |exhibitors_group| %>
<div>
<table>
<tr><th>Refreshments and Exhibits</th></tr>
<% exhibitors_group.each do |exhibitor| %>
<tr>
<td><%= exhibitor.name %></td>
</tr>
<% end %>
</table>
</div>
<% end %>
如果你想要第一个15而其他表中的其余部分执行此操作
<div>
<table>
<tr><th>Refreshments and Exhibits</th></tr>
<% @exhibitor[0..15].each do |exhibitor| %>
<tr>
<td><%= exhibitor.name %></td>
</tr>
<% end %>
</table>
</div>
<div>
<table>
<tr><th>Refreshments and Exhibits</th></tr>
<% @exhibitors[16..-1].each do |exhibitor| %>
<tr>
<td><%= exhibitor.name %></td>
</tr>
<% end %>
</table>
</div>
你还应该考虑两件事: