-1

我有一个Flights出发和到达机场的清单。每个Flight人都有一个出发/到达时间。我想将航班列表显示为时间线,左边是出发时间,右边是到达时间,按小时分组,如下所示:

     Departures  | Arrivals

Hours 08 - 09

Flight 1, 08:15  | Flight ABC, 08:12
Flight 2, 08:21  | Flight XY, 08:36
Flight 05, 08:49 | Flight ABC, 08:38

Hours 09 - 10

Flight 1, 09:25  | Flight ABC, 09:55
Flight 2, 09:56  | 

....

我正在创建这样的哈希:

@flights= {}

Flight.each do |t|
  @flights[t_time] ||= []
  t_time = t.departure_date.change(min: 0)
  if t.departure
    @flights[t_time][0] << t #departures
  else
    @flights[t_time][1] << t #arrivals
  end
end

这似乎奏效了。但是我不知道如何在每次访问视图中解析这个结构,然后访问两个数组中的每个对象。

4

1 回答 1

0

终于想通了。首先,我更改了哈希并对其进行了初始化:

@global_flights[t_time] ||= {}
@global_flights[t_time][:departures] ||= [] # departures
@global_flights[t_time][:arrivals] ||= [] # arrivals

……

Flight.each do |t|
    t_time = t.departure_date.change(min: 0)
    if t.departure
        @global_flights[t_time][:departures] << t
    else
        @global_flights[t_time][:arrivals] << t
    end
end

这允许我在视图中根据需要显示数据:

<% @global_flights.each do |times, flights| %>
    <%= times %>
    <% flights[:departures].each do |t| %>
        <%= render partial: "flight", locals: {t: t} %>
    <% end %>
    <% flights[:arrivals].each do |t| %>
        <%= render partial: "flight", locals: {t: t} %>
    <% end %>
<% end %>
于 2014-07-25T10:05:32.037 回答