如何根据工作日Ruby on Rails显示数据

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何根据工作日Ruby on Rails显示数据相关的知识,希望对你有一定的参考价值。

我正在开发一个界面,我需要根据工作日显示数据。

由于某些原因,它错误地显示了它们。我的要求是在星期一到星期日的每一行显示7个方框,并在那些日期的方框中显示数据,这些方框中保存了数据。

这是我正在使用的代码:

    <% groups.each do |subgroups| %>
     <tr id="slot-formula-<%= subgroups.map(&:id) %>" class="subgroups move_row" title="Drag Drop To Sort Slot Formula Positions">
      <td class="graykbg">
      <p class="doorman" style="margin-top: 3px;color: #353e48;padding: 3px;">
      <%= subgroups.map(&:doorman).first == 'none' ? 'Standard' : subgroups.map(&:doorman).first.upcase %>
      </p>
     <p class="set_doorman" style="display: none;"><%= subgroups.map(&:doorman).first %></p>
    </td>
     <% weeks.zip(subgroups.sort_by {|week_day| weeks.index(week_day.day)}).each do |day, u| %>
    <% puts day.inspect, '***********************' %>
    <% puts u.inspect, '!!!!!!!!!!!!!!!!!!!!!!' %>
     <% if day == u.try(:day) %>
     <td class="timeslots" data-id="<%= u.id %>">
     <span class="slotcheck" data-id="<%= u.id %>" data-day="<%= u.day %>"></span>
      <div>
      <p class="slots">
      <span class="slot_info"><%= "PickUp Slots: #{u.pickup_slots}" %>
      <br></span>
      <span class="slot_info"><%= "DropOff Slots: #{u.dropoff_slots}" %>
       <br></span>
      <span class="slot_info"><%= "Max Visits: #{u.max_visits}" %>
       <br></span>
       <span class="slot_info"><%= "Start Time: #{u.start_time}" %>
       <br></span>
       <span class="slot_info"><%= "End Time: #{u.end_time}" %>
       <br></span>
       <span class="slot_info"><%= "Cut Off Time: #{u.cutoff_time}" %>
       <br></span>
      <span class="slot_info"><%= "Turnaround Time: #{u.turnaround_time}" %>
      <br></span>
     <span class="slot_info"><%= "Day: #{u.day}" %></span>
      </p>
    </div>
      </td>
    <% else %>
   <td class="timeslots">
    <span class="slotcheck"></span>
     <div>
     <p class="slots">
     <span class="slot_info">1
     <br></span>
      <span class="slot_info">2
     <br></span>
    <span class="slot_info">3
    <br></span>
     <span class="slot_info">4
    <br></span>
    <span class="slot_info">5
      <br></span>
     <span class="slot_info">6
     <br></span>
    <span class="slot_info">7
     <br></span>
     <span class="slot_info">8</span>
     </p>
     </div>
   </td>
    <% end %>
   <% end %>
    <%  puts '---------------------------------------' %>
       <td>
      <button class="btn btn-sm color-red slotremove"><span class="fa fa-close"></span>
     </button>
      </td>
     </tr>
    <% end %>

我的代码输出是:

enter image description here

这里是puts语句的控制台结果。

控制台显示有4天数据应该正确显示并将其余数据留空,但它只显示星期一。

因此,基于这些结果,我希望星期一,星期三,星期四和星期五应该有数据框,其余的框应该保持空白。

"Monday"
***********************
#<SlotFormula id: 1889, day: "Monday", pickup_slots: 1, dropoff_slots: 2, max_visits: 20, start_time: " 10:00 PM", end_time: " 11:00 PM", doorman: "none", created_at: "2018-03-05 12:07:54", updated_at: "2018-03-05 12:07:54", status: true, zone_id: 1, cutoff_time: "4:00 PM", turnaround_time: 36, position: 11>
!!!!!!!!!!!!!!!!!!!!!!
"Tuesday"
***********************
#<SlotFormula id: 1890, day: "Wednesday", pickup_slots: 3, dropoff_slots: 4, max_visits: 20, start_time: " 10:00 PM", end_time: " 11:00 PM", doorman: "none", created_at: "2018-03-05 12:08:21", updated_at: "2018-03-05 12:08:21", status: true, zone_id: 1, cutoff_time: "4:00 PM", turnaround_time: 36, position: 10>
!!!!!!!!!!!!!!!!!!!!!!
"Wednesday"
***********************
#<SlotFormula id: 1892, day: "Thursday", pickup_slots: 7, dropoff_slots: 8, max_visits: 20, start_time: " 10:00 PM", end_time: " 11:00 PM", doorman: "none", created_at: "2018-03-05 12:09:25", updated_at: "2018-03-05 12:09:25", status: true, zone_id: 1, cutoff_time: "4:00 PM", turnaround_time: 36, position: 8>
!!!!!!!!!!!!!!!!!!!!!!
"Thursday"
***********************
#<SlotFormula id: 1891, day: "Friday", pickup_slots: 5, dropoff_slots: 6, max_visits: 20, start_time: " 10:00 PM", end_time: " 11:00 PM", doorman: "none", created_at: "2018-03-05 12:09:01", updated_at: "2018-03-05 12:09:01", status: true, zone_id: 1, cutoff_time: "4:00 PM", turnaround_time: 36, position: 9>
!!!!!!!!!!!!!!!!!!!!!!
"Friday"
***********************
nil
!!!!!!!!!!!!!!!!!!!!!!
"Saturday"
***********************
nil
!!!!!!!!!!!!!!!!!!!!!!
"Sunday"
***********************
nil
!!!!!!!!!!!!!!!!!!!!!!
答案

您的日志输出显示问题,您使用zip创建一个数组进行迭代,但结果数组较小,因此日期不排队:

"Tuesday"
***********************
#<SlotFormula id: 1890, day: "Wednesday"

而不是压缩,您应该映射结果集以创建以日为键的哈希。

使用该哈希,您可以迭代这些日子并通过使用日期键的哈希查找来访问结果。

另一答案

@phoet为我提供了正确显示数据的方向。虽然我无法完全理解他的方法并且也问过,但它让我做了我想要实现的目标。

这是我在进行某些重构后的最终工作代码。可以有许多可能/最好的方法来以更有效的方式完成这项工作。

<% available_days = {} %>
 <% week_days = %w(Monday Tuesday Wednesday Thursday Friday Saturday Sunday) %>
 <% all_days = {0 => 'Monday', 1 => 'Tuesday', 2 => 'Wednesday', 3 => 'Thursday', 4 => 'Friday', 5 => 'Saturday', 6 => 'Sunday'} %>
  <% groups.each do |subgroups| %>
  <tr id="slot-formula-<%= subgroups.map(&:id) %>" class="subgroups move_row" title="Drag Drop To Sort Slot Formula Positions">
  <td class="graykbg">
  <p class="doorman" style="margin-top: 3px;color: #353e48;padding: 3px;">
  <%= subgroups.map(&:doorman).first == 'none' ? 'Standard' : subgroups.map(&:doorman).first.upcase %>
  </p>
  <p class="set_doorman" style="display: none;"><%= subgroups.map(&:doorman).first %></p>
  </td>
  <% available_data = subgroups.sort_by {|week_day| week_days.index(week_day.day)}.each_with_index {|record, index| available_days.store(index, record.day)} %>
  <% all_days.each do |key, day| %>
  <% if available_days.has_value?(day) %>
  <% subgroup = available_data.map.select {|added_day| day == added_day.day}.first %>
  <td class="timeslots" data-id="<%= subgroup.id %>">
  <span class="slotcheck" data-id="<%= subgroup.id %>" data-day="<%= subgroup.day %>"></span>
  <div>
  <p class="slots">
  <span class="slot_info"><%= "PickUp Slots: #{subgroup.pickup_slots}" %>
  <br></span>
  <span class="slot_info"><%= "DropOff Slots: #{subgroup.dropoff_slots}" %>
  <br></span>
 <span class="slot_info"><%= "Max Visits: #{subgroup.max_visits}" %>
  <br></span>
  <span class="slot_info"><%= "Start Time: #{subgroup.start_time}" %>
  <br></span>
  <span class="slot_info"><%= "End Time: #{subgroup.end_time}" %>
 <br></span>
 <span class="slot_info"><%= "Cut Off Time: #{subgroup.cutoff_time}" %>
 <br></span>
 <span class="slot_info"><%= "Turnaround Time: #{subgroup.turnaround_time}" %>
  <br></span>
  <span class="slot_info"><%= "Day: #{subgroup.day}" %></span>
  </p>
  </div>
  </td>
  <% else %>
  <td><span class="slotcheck" data-id="" data-day="<%= day %>"></span>
  <div></div>
  </td>
  <% end %>
  <% end %>
  <td>
  <button class="btn btn-sm color-red slotremove"><span class="fa fa-close"></span>
  </button>
  </td>
  </tr>
  <% end %>

代码的输出是(我想要的输出):

只有那些需要填入数据的盒子。现在,管理员可以在空盒子中添加数据,因为它是初始要求的一部分,并且它们将在相应的日期下方以相同的顺序添加。

enter image description here

以上是关于如何根据工作日Ruby on Rails显示数据的主要内容,如果未能解决你的问题,请参考以下文章

Ruby on Rails 中的会话如何工作?

Ruby on Rails 变量在视图中显示 Active Record 数据而不是整数

如何在 ruby​​ on rails 中发布数据

如何在我的 Ruby on Rails 应用程序上使用 ruby​​ 2.7

Ruby on Rails 6 - 如何根据特定路线建模/隐藏视图?

Ruby on Rails Bootstrap Glyphicons 不工作