下面的代码显示每小时约会的次数,如果没有预约可用,则显示'不约会'。没有预约的团体
欲组连续“否预约”插槽根据时间
为例如,当前代码的输出为 -
10:00 AM to 11:00 AM -> No Appointment 11:00 AM to 12:00 PM -> No Appointment 12:00 PM to 01:00 PM -> No Appointment
我想输出是
10:00 AM to 01:00 PM -> No Appointment
该代码如下: -
<% @time_group = ['6:00:00','7:00:00','8:00:00','9:00:00','10:00:00','11:00:00','12:00:00','13:00:00','14:00:00','15:00:00','16:00:00','17:00:00','18:00:00','19:00:00','20:00:00','21:00:00','22:00:00','23:00:00'] %>
#Loop through each hour in @time_group
<% @time_group.each_with_index do |t,index| %>
<% t1 = Time.parse(t) %>
<% t2 = Time.parse(t) + 3600 %>
<% @booked = Appointment.where(start_time: t1..t2 %>
#Show time interval e.g 1:00 PM to 2:00 PM
<%= Time.parse(t).strftime('%I:%M %p') %> to <%= (Time.parse(t) + 3600).strftime('%I:%M %p') %>
#if appointment is available show appointment else show no appointment
<% if @booked.size > 0 %>
<% @booked.each do |app| %>
<%= app.start_time.strftime('%I:%M') %>
<% end %>
<% else %>
No Appointment
<% end %>
<% end %>
好上心..谢谢 – vidal