2

我有 git clone fullcalendar_assets 并且它运行顺利并且事件显示。

我的应用程序 rails 3.2.6 使用 dbms 的 postgresql。我尝试在我的应用程序中应用 fullcalendar。但事件没有出现。

切中要害。

在路线.rb

namespace admin do
 resources :events
end

在 admin/events_controller.rb

class Admin::EventsController < ApplicationController
  include Tenantable::Schema::Controller

  before_filter :authenticate_admin!

  def index
    @events = Event.scoped
    @events = Event.between(params['start'], params['end']) if (params['start'] && params['end'])

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: ([:admin, @events]) }
    end
  end

在 event.rb 上

class Event < ActiveRecord::Base
  include Tenantable::Schema::Model

  scope :between, lambda {|start_time, end_time|
    {:conditions => [
  "starts_at > ? and starts_at < ?",
  Event.format_date(start_time), Event.format_date(end_time)
] }
  }

  # need to override the json view to return what full_calendar is expecting.
  # http://arshaw.com/fullcalendar/docs/event_data/Event_Object/
  def as_json(options = {})
    {
      :id => self.id,
      :nama => self.nama,
      :keterangan=> self.keterangan || "",
      :start => starts_at.rfc822,
      :end => ends_at.rfc822,
      :allDay => self.all_day,
      :recurring => false,
      :url => Rails.application.routes.url_helpers.adminsekolah_event_path(id),
      #:color => "red"
    }

  end

  def self.format_date(date_time)
    Time.at(date_time.to_i).to_formatted_s(:db)
  end

end

在 events.js.coffee 上

$(document).ready ->
  $('#calendar').fullCalendar
    editable: true,
    header:
      left: 'prev,next today',
      center: 'title',
      right: 'month,agendaWeek,agendaDay'
    defaultView: 'month',
    height: 500,
    slotMinutes: 30,

    eventSources: [{
      url: '/admin/events',
    }],

    timeFormat: 'h:mm t{ - h:mm t} ',
    dragOpacity: "0.5"

    eventDrop: (event, dayDelta, minuteDelta, allDay, revertFunc) ->
      updateEvent(event);

    eventResize: (event, dayDelta, minuteDelta, revertFunc) ->
      updateEvent(event);


updateEvent = (the_event) ->
  $.update "/events/" + the_event.id,
    event:
      nama: the_event.nama,
      starts_at: "" + the_event.start,
      ends_at: "" + the_event.end,
      keterangan: the_event.keterangan

我尝试在数据库中添加一个事件和事件(模式:子域/不公开),它很顺利,但我运行 localhost:3000/admin/events ,事件没有出现在日历上(不是错误)

以下命令,例如

Started GET "/admin/events?start=1353776400&end=1357405200&_=135557072567
2" for 127.0.0.1 at 2012-12-15 18:25:25 +0700
Processing by Admin::EventsController#index as JSON
Parameters: {"start"=>"1353776400", "end"=>"1357405200", "_"=>"1355570725672"}
Admin Load (2.0ms)  SELECT "public"."admins".* FROM "public"."admins" WHERE "public"."admins"."id" = 25 LIMIT 1
Event Load (2.0ms)  SELECT "events".* FROM "events" WHERE (starts_at > '2012-11-25 00:00:00' and starts_at < '2013-01-06 00:00:00')
Completed 200 OK in 10ms (Views: 3.0ms | ActiveRecord: 4.0ms)

我尝试在 psql 中查询

education_development=# SELECT * FROM subdomain.events WHERE (starts_at > '2012-11-25 00:00:00' and starts_at < '2013-01-06 00:00:00')
education_development-# ;
id |     name      |       starts_at        |        ends_at         | all_day|   description    |         created_at         |         updated_at
----+---------------+------------------------+------------------------+---------+-----------------+----------------------------+----------------------------
1 | New Year 2013 | 2013-01-01 00:00:00+07 | 2013-01-01 00:00:00+07 | f | New Year 2013 | 2012-12-15 06:53:50.695456 | 2012-12-15 06:53:50.695456
(1 row)

对这个问题有什么想法吗?

4

1 回答 1

2

Apparently this is because the JSON returned from the controller, it should return an array of events but instead it returns an array of 'admin' as the first element and array of events as the second element, which fullcalendar doesn't expect. Simply use

      format.json { render json: @events }

and it should work.

UPDATE: Also you localized the hash keys of the JSON representation of your event object, you must use the the same keys in fullcalendar model/event.rb.

于 2012-12-15T16:12:02.473 回答