I want to show two columns of the code in table data tag"" per row while using '.each' method. But the issue is that the following code displays one column in a row.
<table>
<% @lease.apartment.roommates.each do |roommate| %>
<tr>
<td colspan="5">
<% unless roommate == @lease.second_occupant || roommate == @lease.user %>
<% if roommate.current_room.present? %>
<p>
<%= roommate.full_name %> -
<% if roommate.current_room.apartment == @lease.apartment%>
<%= roommate.current_room&.label %>
<% end %>
<br>Email:<%= roommate.email %><br>Phone:<%= roommate.phone %><br>
<% if @lease.end_at.present? %>
Lease End date (if applicable):<%= @lease.end_at %>
<% end %>
</p>
<% end %>
<% end %>
</td>
</tr>
<% end %>
</table>
You can use the each_slice method for dividing the array into slices of 2 elements each:
<table>
<% @roommates.each_slice(2) do |roommate_slice| %>
<tr>
<td colspan="5">
<%= roommate_slice[0].full_name %>
<%= roommate_slice[1].full_name %>
</td>
</tr>
<% end %>
</table>
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.