循环遍历日期整数

时间:2014-02-18 11:48:55

标签: ruby-on-rails loops

我有以下代码:

<span class="bookings">
    <span class="col-md-2">
      <h4><%= time_tag(Date.today + 1.days) %></h4>
      <% @booking.each do |b| %>
        <% if b.date == Date.today + 1.days && b.type == "Hot Desk"  %>
          <% if b.desk == @hotdesk.code %>
            <span class="glyphicon glyphicon-remove booked-show"></span>
          <% end %>
        <% end %>
      <% end %>
    </span>
  </span>

  <span class="bookings">
    <span class="col-md-2">
      <h4><%= time_tag(Date.today + 2.days) %></h4>
      <% @booking.each do |b| %>
        <% if b.date == Date.today + 2.days && b.type == "Hot Desk"  %>
          <% if b.desk == @hotdesk.code %>
            <span class="glyphicon glyphicon-remove booked-show"></span>
          <% end %>
        <% end %>
      <% end %>
    </span>
  </span>

这延续了很多天,我想显示预订的可用性。我想要实现的只是将这些代码块中的一个编写出来并让它循环显示 Date.today + 1.days ,并将每个循环的日期值增加1到20次左右。

有关此方法的任何想法可以让我一次又一次地编写此代码吗?

1 个答案:

答案 0 :(得分:1)

<span class="bookings">
    <% 1.upto(YOUR_COUNT.to_i).each do |day_count| %>
    <span class="col-md-2">
      <h4><%= time_tag(Date.today + day_count.days) %></h4>
      <% @booking.each do |b| %>
        <% if b.date == Date.today + day_count.days && b.type == "Hot Desk"  %>
          <% if b.desk == @hotdesk.code %>
            <span class="glyphicon glyphicon-remove booked-show"></span>
          <% end %>
        <% end %>
      <% end %>
    </span>
    <% end %>
  </span>