简体   繁体   English

遍历日期整数

[英]Looping through a date integer

I have the following code: 我有以下代码:

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

Which extends on to however many days I want to display availability of bookings for. 无论如何,我想显示多少天的预订情况。 What I would like to achieve is writing only one of these blocks of codes out and having it loop through where it says Date.today + 1.days and increase the value of the days by 1 each loop up to 20 or so times. 我要实现的是只写出其中的一块代码,并使它遍历显示Date.today + 1.days的地方,然后将天的值增加1,每次循环最多20次左右。

Any ideas on a way to do this to save me writing this code over and over again? 有什么想法可以避免我一遍又一遍地编写此代码吗?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM