从rails模型动态创建一个ics / ics文件(dynamically create an ical / ics file from a rails model)
鉴于我需要将所有信息存储在rails应用程序中的:event模型中。 如何向视图添加按钮,单击该按钮时会提供动态创建的ics / ics文件?
我环顾了几天,我发现的所有信息似乎已经过时或假设我已经知道如何做大块的解决方案。
Given that I have all the info I need stored in an :event model in a rails app. How do I add a button to a view which when clicked delivers a dynamically created an ical/ics file?
I've looked around for a few days and all the info I'm finding seems out of date or assumes that I already know how to do big chunks of the solution.
最满意答案
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: filenameThe icalendar gem works well. https://github.com/icalendar/icalendar
I use this to generate outlook and ical versions. Works great.
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更多推荐
发布评论