循环并生成表格单元格

时间:2012-11-01 18:39:05

标签: ruby-on-rails-3

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>

其中输出以下内容:

the output

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

更新版本

<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>

1 个答案:

答案 0 :(得分:1)

你过早地结束你的循环,你需要一个td来改变每一个:

<% @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 %>