我有一张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
这似乎有效。但是我不知道如何在视图中解析这个结构以便每次访问,然后在两个数组中的每个对象之后。任何帮助将不胜感激。
答案 0 :(得分: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 %>