1

我将 FullCalendar 与 rails3.2.5 一起使用。我将 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

我怎么解决这个问题。请帮我。提前致谢。

4

1 回答 1

0

我也使用 mongoid 和 fullcalendar。无法真正测试您的代码,但我在模型方面没有做任何特别的事情。这就是我让它工作的方式..

显示.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 
于 2014-04-04T02:08:01.903 回答