Home > Software design >  In ruby on rails how to change table format
In ruby on rails how to change table format

Time:03-27

hi I am new to rails pls need help.

Table data from index.html.erb

<table >                         
 <thead>  
  <tr> 
    <th>Area</th> 
    <th>Item</th>
    <th>Year</th>
    <th>Value</th>
  </tr>
</thead>
<tbody>
  <%@a1s.each do |ada| %>
    <tr>
      <td><%=ada.Area%></td>
      <td><%=ada.Item%></td>
      <td><%=ada.Year%></td>
      <td><%=ada.Value%></td>
    </tr>
  <%end%> 
</tbody>
</table> 

index.html.erb

how to change index.html.erb table to example converted table

  1. convert year data as a header and show their value.
  2. Item and Area names show ones and next country shown in next row.

example converted table

CodePudding user response:

convert year data as a header and show their value.

You are looking for a list of all years in the data. You could do that, for example, with:

@a1s.map(&:Year).uniq

Item and Area names show ones and next country shown in next row.

I'd advise using .group_by to organise the data for each row in the table.

<%@a1s.group_by(&:Area).each do |area, adas_in_area| %>
  <           
  • Related