Fullcalendar Rails- Event does not appear on the calendar

醉酒当歌 提交于 2019-12-11 03:13:28

问题


i have git clone fullcalendar_assets and it's running smoothly and event showing.

my application rails 3.2.6 using postgresql for dbms. I try to apply fullcalendar in my application. but event not appear.

to the point.

on 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

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

i try to add one event and event put on database (schema : subdomain / not public), it's smoothly but i run localhost:3000/admin/events , event does not appear on the calendar (not error)

following command such as

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)

i try the query in 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)

any idea for this issue?


回答1:


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.



来源:https://stackoverflow.com/questions/13893107/fullcalendar-rails-event-does-not-appear-on-the-calendar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!