Fullcalendar Rails-事件不会出现在日历上 [英] Fullcalendar Rails- Event does not appear on the calendar
问题描述
我的应用程序rails 3.2.6使用postgresql for dbms。我尝试在我的应用程序中应用fullcalendar。但事件不会出现。
到点。
位于routes.rb
命名空间admin do
资源:events
end
on admin / events_conttroler.rb
class Admin :: EventsController< ApplicationController
包括Tenantable :: Schema :: Controller
before_filter:authenticate_admin!
def index
@events = Event.scoped
@events = Event.between(params ['start'],params ['end'])if(params ['开始']&& params ['end'])
respond_to do | format |
format.html#index.html.erb
format.json {render json:([:admin,@events])}
end
end
code>
on event.rb
class Event< ActiveRecord :: Base
包括Tenantable :: Schema :: Model
scope:between,lambda {| start_time,end_time |
{:conditions => [
starts_at>?和starts_at<?,
Event.format_date(start_time),Event.format_date(end_time)
]}
}
#需要重写json视图以返回full_calendar期望的内容。
#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 =>假,
: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 :(事件,dayDelta,minuteDelta,allDay,revertFunc) - >
updateEvent(event);
eventResize :(事件,dayDelta,minuteDelta,revertFunc) - >
updateEvent(event);
updateEvent =(the_event) - >
$ .update/ events /+ the_event.id,
事件:
nama:the_event.nama,
starts_at:+ the_event.start,
ends_at:+ the_event.end,
keterangan:the_event.keterangan
我尝试添加一个事件和事件放在数据库(架构:子域/不公开),它很顺利,但我运行本地主机:3000 /管理/事件,事件不会出现在日历上(不错误)
$
已启动GET/ admin / events?start = 1353776400& end = 1357405200& _ = 135557072567
2for 127.0.0.1 at 2012-12-15 18:25:25 +0700
处理由Admin :: EventsController#index为JSON
参数:{start =1353776400,end=>1357405200,_=>1355570725672}
管理负荷(2.0ms)SELECTpublic。admins。* FROMpublic 。adminsWHEREpublic。admins。id= 25 LIMIT 1
事件载入(2.0ms)SELECTevents。* FROMeventsWHERE(starts_at> '20 12-11-25 00:00:00'和starts_at< '2013-01-06 00:00:00')
在10ms内完成200 OK(查看: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 |名称| starts_at | ends_at | ALL_DAY |描述| created_at | updated_at
---- + --------------- + ------------------------ + ------------------------ + --------- + -------------- --- + ---------------------------- + ----------------- -----------
1 | 2013年新年| | 2013-01-01 00:00:00 + 07 | 2013-01-01 00:00:00 + 07 | f | 2013年新年| | 2012-12-15 06:53:50.695456 | 2012-12-15 06:53:50.695456
(1 row)
这个问题?显然这是因为从控制器返回的JSON,它应该返回一个事件数组,但是它会返回作为第一个元素的admin数组和第二个元素的事件数组,fullcalendar并不期望。只需使用
format.json {render json:@events}
$
更新:您还定位了散列键您的事件对象的JSON表示,您必须使用fullcalendar
model / event.rb
中的相同键。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?
解决方案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
.这篇关于Fullcalendar Rails-事件不会出现在日历上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!