John Oggy John Oggy - 1 month ago 11
Ruby Question

How to build a table using HAML

I trying to build a table. Here is my HAML code:

%table{:border => 1}
%tbody
%tr
%td Question:
- @cust_dashboard.each do |object|
%tr
%td= object.question
%tr
%td Status:
- @cust_dashboard.each do |object|
%tr
%td= object.status
%tr
%td Created_at:
- @cust_dashboard.each do |object|
%tr
%td= object.created_at


All the items in the table are in one column:

enter image description here

I need a table with three columns:

enter image description here

Answer

I haven't written HAML for a while, but I think you want:

%table{:border => 1}
  %tbody
    %tr
      %td Question:
      %td Status:
      %td Created_at:
    - @cust_dashboard.each do |object|
      %tr
        %td= object.question
        %td= object.status
        %td= object.created_at

When building your template, think in terms of how the data will be emitted: We'd see:

<table>
  <tbody>
    <tr>
      <td>
      <td>
      <td>
    </tr>

Followed by a series of:

<tr>
  <td>
  <td>
  <td>
</tr>

(indented correctly of course) These would be each data row, three cells across.

I'd probably use th instead of td for the headers though:

%th Question:
%th Status:
%th Created_at: