完整日历和自动过滤事件Rails 4

时间:2013-12-01 20:44:35

标签: ruby-on-rails-4 fullcalendar filtering

我正在构建一个日历应用程序,该应用程序需要根据正在查看的位置在日历上显示某些事件。我有完整的日历工作,它在数据库中显示 ALL 事件。我正在尝试实现一个自动过滤器,仅显示与正在查看的位置相关的事件。

当前设置(我的事件模型称为“广告系列”以与我的应用程序对齐)

广告系列控制器

def index
  @campaigns = Campaign.all
  @campaigns = @campaigns.after(params['start']) if (params['start'])
  @campaigns = @campaigns.before(params['end']) if (params['end'])

  respond_to do |format|
    format.html # index.html.erb
    format.json { render json: @campaigns }

  end
end

广告系列模型

belongs_to :location 

scope :before, lambda {|end_time| {:conditions => ["ends_at < ?",  Campaign.format_date(end_time)] }}
scope :after, lambda {|start_time| {:conditions => ["starts_at > ?", Campaign.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.name,
  :description => "",
  :start => starts_at.rfc822,
  :end => ends_at.rfc822,
  :allDay => false,
  :recurring => false,
  :url => Rails.application.routes.url_helpers.campaign_path(friendly_id)
  }

end

 def self.format_date(date_time)
   Time.at(date_time.to_i).to_formatted_s(:db)
 end

“位置”内的脚本show.html.erb

  $(document).ready(function() {

    var date = new Date();
    var d = date.getDate();
    var m = date.getMonth();
    var y = date.getFullYear();

    $('#calendar').fullCalendar({
            editable: true,
            header: {
        left: 'prev,next today',
        center: 'title',
        right: 'month,agendaWeek,agendaDay'
    },
    defaultView: 'month',

    loading: function(bool){
        if (bool)
            $('#loading').show();
        else
            $('#loading').hide();
    },

    // a future calendar might have many sources.
    eventSources: [{
        url: '/campaigns',
        data: { <%= @locations %> }, #added but not filtering as I had hoped
        color: 'blue',
        textColor: 'white',
        ignoreTimezone: false
    }],

    timeFormat: 'h:mm t{ - h:mm t} ',
    dragOpacity: "0.5",


    });
   });

目前,我可以通过以下方式显示属于显示位置上指定位置的广告系列:

 <strong>Campaigns:</strong>
   <%= render partial: @campaigns %>
 </p>

然后在位置控制器中

  def show
   @campaigns = @location.campaigns
  end

我已经尝试了几个小时来弄清楚这一点,但没有运气来获得相同的结果到日历。有人可以帮助我找出过滤与观看位置相关的“广告系列”所需的内容吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

你试过这个吗?

 eventSources: [{
    url: '/campaigns',
    data: { 'location': <%= @locations %> }, <- try this like so
    color: 'blue',
    textColor: 'white',
    ignoreTimezone: false
}],