kjbradley
kjbradley

Reputation: 335

How to add event sources

In my fullcalendar page, I have a (default) event sources area that looks like such:

eventSources: [{
    url: '/events/index/',
    color: 'yellow',
    textColor: 'black',
    ignoreTimezone: false
}],

I have a model (and view) folder titled "rentalcars". How would I make the events automatically be pulled from that model? This model has start and end dates.

I tried:

eventSources: [{
    url: '/rentalcars/index/',
    color: 'yellow',
    textColor: 'black',
    ignoreTimezone: false
}],

which certainly does not work. I looked at arshaw's source, and I can tell how to make static events, but I am looking for dynamic ones: ones that will iterate through existing models.

Any suggestions?

Upvotes: 0

Views: 388

Answers (1)

Christoph Eicke
Christoph Eicke

Reputation: 1144

Your url parameter needs to return whatever the FullCalendar understands and can parse, so judging by the documentation you will have to make sure that your rentelcars index action returns this particular format:

{
        title  : 'event1',
        start  : '2010-01-01'
    },
    {
        title  : 'event2',
        start  : '2010-01-05',
        end    : '2010-01-07'
    },
    {
        title  : 'event3',
        start  : '2010-01-09 12:30:00',
        allDay : false // will make the time show
    }
}

So in your rentalcars_controller.rb in the index definition you will have to make sure that you return this format which looks a lot like JSON to me. So, easiest way would be to have this as your JavaScript:

eventSources: [{
  url: '/rentalcars.json',
  color: 'yellow',
  textColor: 'black',
  ignoreTimezone: false
}],

And then in your controller you will have something like this:

def index
    rentalcar_dates = RentelcarDates.all # assuming this is an object that holds start, end date and maybe the car name

    respond_to do |format|
        format.html
        format.json { render :json => rentalcar_dates }
    end
end

Upvotes: 1

Related Questions