从rails模型动态创建一个ics / ics文件

时间:2015-06-08 22:33:39

标签: ruby-on-rails icalendar

鉴于我将所有需要的信息存储在rails应用中的:event模型中。如何向视图添加按钮,单击该按钮时会提供动态创建的ics / ics文件?

我环顾了几天,我发现的所有信息似乎已经过时或假设我已经知道如何做大块的解决方案。

1 个答案:

答案 0 :(得分:11)

icalendar gem运作良好。 https://github.com/icalendar/icalendar

我用它来生成outlook和ical版本。效果很好。

cal = Icalendar::Calendar.new
filename = "Foo at #{foo.name}"

if params[:format] == 'vcs'
  cal.prodid = '-//Microsoft Corporation//Outlook MIMEDIR//EN'
  cal.version = '1.0'
  filename += '.vcs'
else # ical
  cal.prodid = '-//Acme Widgets, Inc.//NONSGML ExportToCalendar//EN'
  cal.version = '2.0'
  filename += '.ics'
end

cal.event do |e|
  e.dtstart     = Icalendar::Values::DateTime.new(foo.start_at, tzid: foo.time_zone)
  e.dtend       = Icalendar::Values::DateTime.new(foo.end_at, tzid: foo.course.time_zone)
  e.summary     = foo.summary
  e.description = foo.description
  e.url         = event_url(foo)
  e.location    = foo.formatted_address
end

send_data cal.to_ical, type: 'text/calendar', disposition: 'attachment', filename: filename