1
I have a table that looks like the following: 

<table class = "rota">
    <thead>
        <th>Date</th> 
        <% @hospitals.each do |hosp| %>
        <th><%= hosp.name%></th>
        <% end %>
    </thead>
    <tbody>
        <%- if @rota_days.blank? %>
        <tr>
            <td colspan="<%= @hospitals.count %>">No rota day</td>
        </tr>
        <% end -%>
        <% @dates.each do |date| %>
        <tr>
            <td><%= date.inspect %></td>

            <% end %>
        </tr>
    </tbody>
</table>

输出以下内容:

输出

我正在尝试生成包含空单元格的空白行。我似乎无法确定我哪里出错了。什么是最好的解决方案

更新后的版本

<table class = "rota">
    <thead>
        <th>Date</th> 
        <% @hospitals.each do |hosp| %>
        <th><%= hosp.name%></th>
        <% end %>
    </thead>
    <tbody>
        <%- if @rota_days.blank? %>
        <tr>
            <td colspan="<%= @hospitals.count %>">No rota day</td>
        </tr>
        <% end -%>
        <% @dates.each do |date| %>
        <tr>
            <td><%= date.inspect %></td>
            <% (1..@hospitals.count).each do %> 
                <td></td> 
            <% end %> 
           </tr> 
            <% end %>
    </tbody>
</table>
4

1 回答 1

1

你结束你的循环太快了,你需要一个 td 为每个 th 所以改变:

<% @dates.each do |date| %>
  <tr>
    <td><%= date.inspect %></td>
     <% end %>
  </tr>

至:

<% @dates.each do |date| %>
  <tr>
    <td><%= date.inspect %></td> 
    <% (1..@hospitals.count).each do %>
      <td></td>    
    <% end %>       
  </tr>
<% end %>
于 2012-11-01T18:51:26.683 回答