Fullcalendar Rails-事件未出现在日历上

时间:2012-12-15 14:35:59

标签: ruby-on-rails json ruby-on-rails-3.2 fullcalendar

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

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

到了这一点。

在routes.rb

namespace admin do
 resources :events
end

on admin / events_conttroler.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

on 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)

对此问题的任何想法?

1 个答案:

答案 0 :(得分:2)

显然这是因为从控制器返回的JSON,它应该返回一个事件数组,而是返回一个'admin'数组作为第一个元素和事件数组作为第二个元素,fullcalendar不期望。只需使用

      format.json { render json: @events }

它应该有用。

更新:您还对事件对象的JSON表示的哈希键进行了本地化,您必须在fullcalendar model/event.rb中使用相同的键。