我在rails3.2.5中使用了FullCalendar。我用mongodb数据库和mongoid gem。当我创建事件时,此类事件不会出现在日历中。我的模型(event.rb)如下
class Event
include Mongoid::Document
field :title, type: String
field :starts_at
field :ends_at
field :all_day, type: Boolean
field :description, type: String
scope :before, lambda {|end_time| {:conditions => ["ends_at < ?", Event.format_date(end_time)] }}
scope :after, lambda {|start_time| {:conditions => ["starts_at > ?", Event.format_date(start_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,
:title => self.title,
:description => self.description || "",
:start => starts_at,
:end => ends_at,
:allDay => self.all_day,
:recurring => true,
:url => Rails.application.routes.url_helpers.event_path(id)
}
end
def self.format_date(date_time)
Time.at(date_time.to_i).to_formatted_s(:db)
end
end
我该如何解决这个问题。请帮我。提前谢谢。
答案 0 :(得分:0)
我也使用mongoid和fullcalendar。无法真正测试您的代码,但我不会在模型方面做任何特别的事情。这就是我的工作方式..
show.html.haml
.row
.span12
= render 'shared/flash_app_success'
%center
%h1
All Events
%br/
:javascript
$(document).ready(function(){
$('#calendar').fullCalendar({
editable: true,
droppable: false,
disableDragging: true,
disableResizing: true,
header: {
left: 'prev,next today',
center: 'title',
right: 'month,agendaWeek,agendaDay'
},
defaultView: 'month',
height: 500,
slotMinutes: 15,
loading: function(bool){
if (bool)
$('#loading').show();
else
$('#loading').hide();
},
events: {
url: '/events/get_events',
cache: true,
type: 'POST',
data:{appdata: $('#appdata').val()}
},
timeFormat: 'H:mm { - H:mm } ',
eventClick: function(event, jsEvent, view){
$.get("/events/show_event/", {
"eventid": event.id
}, null, "script");
$("#appevents").modal("show");
},
});
});
事件控制器
def get_events
@apps = current_user.apps.find(params[:appdata]).to_a
apps = []
events = []
color = "#336699"
@apps.each do |app|
app.events.each do |event|
if event.status == 4
color = "#FFA300"
elsif event.status == 5
color = "#FF0000"
else
color = "#3a87ad"
end
events << {:id => event.id,
:title => User.find(app.user_id).name + ": " + event.title,
:start => "#{event.start_time.iso8601}",
:end => "#{event.end_time.iso8601}",
:allDay=>false,
:backgroundColor => color}
end
end
render :text => events.to_json
end