Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

209 lines (135 sloc) 6.843 kb


Easily show multiple, overlapping events across calendar days and rows.

See for a screenshot.

After install, the “calendar” method will be available within your views.

To customize the look, modify the included stylesheet and/or change the default options.


script/plugin install git://

To generate the necessary static files AND the example below:

script/generate event_calendar


  • –static-only: Only generate the stylesheet and javascript

  • –use-jquery: Generate jquery javascript

You can change the default event model name (Event) and view name (Calendar) by passing in two name arguments:

script/generate event_calendar EventModel ViewName

Generated Files

Make sure to include the stylesheet and javascript in your layout/view.

Static files


Unless the –static-only option is given, the following will be generated. Names will differ if name arguments were passed to the generator.


class CreateEvents < ActiveRecord::Migration
  def self.up
    create_table :events do |t|
      t.string :name
      t.datetime :start_at
      t.datetime :end_at


  def self.down
    drop_table :events

At minimum we need to have start_at and end_at fields.

An event can also have a color field (hex value stored as a string) which determines the color of the event. Or simply override the default virtual attribute on the model. For example, if events are associated to a calendar model, then the events can get their color from the calendar.


class Event < ActiveRecord::Base


map.calendar "/calendar/:year/:month", :controller => "calendar", :action => "index", :year =>, :month =>


class CalendarController < ApplicationController

  def index
    @month = params[:month].to_i
    @year = params[:year].to_i

    @shown_month = Date.civil(@year, @month)

    @event_strips = Event.event_strips_for_month(@shown_month)



Some helper methods are created, but you could put this in the view. The key is our calendar method, which takes some options.

module CalendarHelper
  def month_link(month_date)
    link_to(month_date.strftime("%B"), {:month => month_date.month, :year => month_date.year})

  # custom options for this calendar
  def event_calendar_options
      :year => @year,
      :month => @month,
      :event_strips => @event_strips,
      :month_name_text => @shown_month.strftime("%B %Y"),
      :previous_month_text => "<< " + month_link(@shown_month.last_month),
      :next_month_text => month_link(@shown_month.next_month) + " >>"

  def event_calendar
    calendar event_calendar_options do |event|
      %(<a href="/events/#{}" title="#{h(event.title)}">#{h(event.title)}</a>)

Notice you can pass in a block to the calendar method. In this example I'm passing a link to the event details, and displaying the event's name.


Then in calendar view, simply:

<%= event_calendar %>

Default Options

The default options for the calendar are:

defaults = {
  :year =>,
  :month =>,
  :abbrev => (0..2),
  :first_day_of_week => 0, # See note below when setting this
  :show_today => true,
  :month_name_text =>"%B %Y"),
  :previous_month_text => nil,
  :next_month_text => nil,
  :event_strips => [],

  # it would be nice to have these in the CSS file
  # but they are needed to perform height calculations
  :width => nil,
  :height => 500, 
  :day_names_height => 18,
  :day_nums_height => 18,
  :event_height => 18,
  :event_margin => 1,
  :event_padding_top => 1,

  :use_javascript => true,
  :link_to_day_action => false

You can override any of these by passing your options to the calendar method. In the above example, update the event_calendar_options helper method.


  • See the notes in the plugin's calendar_helper.rb for more info.

  • width: Optional, if none is given it will stretch to the containing element.

  • height: Defaults to 500px. This is the approx minimum total height of the calendar. It could be greater if a calendar row(s) need to stretch to fit additional events.

  • use_javascript: If you set it to false, cleaner HTML will be output, without inline Javascript, but events spanning multiple days will not be displayed correctly. This option should therefore only be used if your calendar uses only single-day events.

  • link_to_day_action: Will make the calendar's day numbers links to the given Rails action. Note, you'll probably want a corresponding route, controller action, and view to go with this action. Example:

    Helper/View calendar option:

    :link_to_day_action => "day"

    Route (the controller is the same as your other calendar route):

    map.calendar_day "/calendar/:year/:month/:day", :controller => "calendar", :action => "day"


  • If you want to change the first day of the week from the default of Sunday (0), then set the new value in an instance variable and pass it to event_strips_for_month (in the controller), and to the event calendar options (in the helper/view).


    @first_day_of_week = 1
    @event_strips = Event.event_strips_for_month(@shown_month, @first_day_of_week)

    Helper/View calendar options:

    :first_day_of_week => @first_day_of_week
  • If you need access to the events, not just the event strips, then instead of calling event_strips_for_month call these 3 methods:

    start_d, end_d = Event.get_start_and_end_dates(@shown_month) # optionally pass in @first_day_of_week
    @events = Event.events_for_date_range(start_d, end_d)
    @event_strips = Event.create_event_strips(start_d, end_d, @events)
  • The event select color is set in the event_calendar.js file.


  • Jeff Schuil

  • See commit history for list of additional contributors.

  • Thanks to those who have added features, fixed bugs, and/or reported issues.


Copyright © 2009 Elevation, released under the MIT license

Jump to Line
Something went wrong with that request. Please try again.