ICalendar is a standard (RFC 2445) for calendar data exchange. The standard is sometimes referred to as "iCal", which also is the name of the Apple, Inc. calendar program that provides one of the implementations of the standard. Source: ICalendar.

It's possible to generate ICalendar files with Ruby on Rails. This can be useful for letting users adding your events in their calendars.

The first step is the install of the icalendar gem: gem install icalendar. An alternative can be vpim.

Then, this library must be required from rails. Add require 'icalendar' to your config/environment.rb file. You can require it from elsewhre, but I think that config/environment.rb is a good place.

Here, we are ready to code our export of events fo .ics file. We suppose that we have an Events model:

  1. class Event < ActiveRecord::Base
  2. # Table name: events
  3. #
  4. #  id           :integer(11)     not null, primary key
  5. #  title        :string(255)
  6. #  date         :datetime
  7. #  end_date     :datetime
  8. #  summary      :text
  9. #  content      :text
  10. #  created_at   :datetime
  11. #  updated_at   :datetime
  12. end

We add a to_ics to this class:

  1. # Convert to iCalendar
  2. def to_ics
  3. event = Icalendar::Event.new
  4. event.start = self.date.strftime("%Y%m%dT%H%M%S")
  5. event.end = self.end_date.strftime("%Y%m%dT%H%M%S")
  6. event.summary = self.title
  7. event.description = self.summary
  8. event.location = 'Here !'
  9. event.klass = "PUBLIC"
  10. event.created = self.created_at
  11. event.last_modified = self.updated_at
  12. event.uid = event.url = "#{PUBLIC_URL}events/#{self.id}"
  13. event.add_comment("AF83 - Shake your digital, we do WowWare")
  14. event
  15. end

Don't forget to declare PUBLIC_URL. I've done this declaration in config/environment/production.rb, so I can have different URL for development and production.

  1. PUBLIC_URL = "http://my.site.com/"

The next part is modifying the show action of the EventsController to accept .ics format:

  1. def show
  2. @event = Event.find(params[:id]})
  3.  
  4. respond_to do |wants|
  5. wants.html
  6. wants.ics do
  7. calendar = Icalendar::Calendar.new
  8. calendar.add_event(@event.to_ics)
  9. calendar.publish
  10. render :text =&gt; calendar.to_ical
  11. end
  12. end
  13. end

Note: the ics format is known by rails 2.0. If it was not the case, we could add it to config/initializes/mime_types.rb.

Add a link to your ical export and we are done:

  1. <%= link_to @event.title, :controller => 'events', :action => :show, :format => :ics %>

You can now enjoy your icalendar events. There are some interresting links if you want more informations:

http://dev.af83.com/trackback/36
Monica on Mon, 02/15/2010 - 04:19

Thanks for this post. This was actually really useful. It's crazy just how undocumented icalendar is...

Green Rails on Tue, 04/15/2008 - 13:39

I wanted to thank you for this great post (and several others). In a world of partially documented things, it's really (really) helpful to have more complete coverage.

Tom