Home > Net >  How to place two columns in a row while using .each method in rails
How to place two columns in a row while using .each method in rails

Time:09-19

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>

CodePudding user response:

You can do it this way so that you get two columns in a row

<table>
<% @lease.apartment.roommates.each_with_index do |roommate, i| %>
  <% if (i 1)%2 == 1%>
  <tr>
  <% end %>
    <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>
  <% if (i 1)%2 == 0%>
  </tr>
  <% end %>

<% end %>

CodePudding user response:

Hi you can use each_slice as mentioned in the comment by tadman

<table>
  <% @lease.apartment.roommates.each_slice(2) do |roommate_pair| %>
    <% roommate_pair.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 %><be>
                <% if @lease.end_at.present? %>
                  Lease End date (if applicable):<%= @lease.end_at %>
                <% end %>
              </p>
            <% end %>
          <% end %>
        </td>
      </tr>
    <% end %>
  <% end %>
</table>

I hope this will help you.

  • Related