I have the following table called products:
|id| |name|
81 monitor
82 keyboard
83 mouse
84 headphone
The controller is products_controller.rb with the following code:
def index
@products = Product.all
end
It should display the following result
|Nº| |name| |Nº| |name|
1 monitor 3 mouse
2 keyboard 4 headphone
I tried the following code using SLICE:
<table>
<tr>
<td>Nº</td>
<td>Product</td>
</tr>
<% @products.each_slice(2).each_with_index do |grouped_array,row_index| %>
<tr>
<% grouped_array.each_with_index do |array, column_index| %>
<td><%= column_index + 1 %></td>
<td><%= array.name %></td>
<% end %>
</tr>
<% end %>
</table>
The result using SLICE was:
|Nº| |name| |Nº| |name|
1 monitor 1 mouse
2 keyboard 1 headphone
I tried using in_groups_of
<% @products.in_groups_of(2).each_with_index do |grouped_array,row_index| %>
<tr>
<% grouped_array.each_with_index do |array, column_index| %>
<td><%= column_index + 1 %></td>
<td><%= array.name %></td>
<% end %>
</tr>
<% end %>
The result using in_groups_of was:
|Nº| |name| |Nº| |name|
1 monitor 1 mouse
2 keyboard 1 headphone