我有一个存储过程,它在单个数组中返回以下哈希值:
@cars = [{"make"=>"honda"}, {"color"=>"black"}, {"make"=>"acura"}, {"color"=>"red"}]
我如何迭代这些中的每一个,以便我可以正确地将它们放入表格中。导致:
<table>
<tr>
<td>honda</td>
<td>black</td>
</tr>
<tr>
<td>acura</td>
<td>red</td>
</tr>
</table>
答案 0 :(得分:2)
我会改变ruby中的数据格式:
@good_cars = @cars.each_slice(2).map { |a,b| a.merge(b) }
# returns [{"make"=>"honda", "color"=>"black"}, {"make"=>"acura", "color"=>"red"}]
答案 1 :(得分:1)
<table>
<% @cars.each_slice(2) do |hash1, hash2| %>
<tr>
<td><%= hash1['make'] %></td>
<td><%= hash2['color'] %></td>
</tr>
<% end %>
</table>