2016-09-21 2 views
0

icalendar gemを使用して、任意の公開GoogleカレンダーICSエクスポートを解析し、それらをRailsアプリケーションに表示します。問題は、イベントが逆アルファベット順に表示されていることです。私は開始時刻(dtstart)で時間順にソートする方法を理解しようとしています。Ruby/Rails:dtstartでicalendarファイルのデータをソートする方法は?

コントローラー:

require 'icalendar' 
require 'net/https' 

uri = URI('https://calendar.google.com/calendar/ical/7d9i7je5o16ec6cje702gvlih0hqa9um%40import.calendar.google.com/public/basic.ics') 
# above is an example calendar of Argentinian holidays 
# the actual calendar would theoretically have hour/minute start/end times 
calendar = Net::HTTP.get(uri) 
cals = Icalendar::Calendar.parse(calendar) 
cal = cals.first 
@holidays = cal.events 

ビュー:

<% @holidays.each do |x| %> 
     <div class="event"> 
      <div class="event-title"><strong><%= x.summary.upcase %></strong></div> 
      <div class="event-room">Room <%= x.location %><span class="event-time"><%= x.dtstart.strftime('%I:%M%p') + ' to ' + x.dtend.strftime('%I:%M%p') %></span> 
      </div> 
     </div> 
<% end %> 

これは逆のアルファベット順ではなく、(好ましくはDTSTARTによる)時系列順にDOMにレンダリングするイベントになります。

ICalendar内のsort_by!メソッドは未定義です。

答えて

0

これはわかりました。そのようなコントローラをやり直し:

require 'icalendar' 
require 'net/http' 

    uri = URI('https://calendar.google.com/calendar/ical/7d9i7je5o16ec6cje702gvlih0hqa9um%40import.calendar.google.com/public/basic.ics') 
    calendar = Net::HTTP.get(uri) 
    cals = Icalendar::Calendar.parse(calendar) 
    cal = cals.first 
    all_events = cal.events 
    @sorted_events = all_events.sort! { |a,b| a.dtstart <=> b.dtstart } 

次に、あなたのビューに代わり@holidaysの@sorted_eventsを使用します。

<% @sorted_events.each do |x| %> 
     <div class="event"> 
      <div class="event-title"><strong><%= x.summary.upcase %></strong></div> 
      <div class="event-room">Room <%= x.location %><span class="event-time"><%= x.dtstart.strftime('%I:%M%p') + ' to ' + x.dtend.strftime('%I:%M%p') %></span> 
      </div> 
     </div> 
<% end %> 

トリックを行う必要があること。 irbでは.sort!メソッドが機能しません。

関連する問題