Jquery FullCalendar-根据ruby app中的even_type更改事件颜色

我在我的ruby应用程序中使用来自http://arshaw.com/fullcalendar的 Jquery完整日历。 我想为4种不同类型的活动展示不同的颜色,例如(假期,学校,工作,娱乐),这些活动在我的事件表中保存为type_of_event

现在我只使用start_at和end_at使用下面的代码获取事件:

scope :before, lambda {|end_time| {:conditions => ["ends_at  ["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.rfc822, :end => ends_at.rfc822, :allDay => self.all_day, :recurring => false, :url => Rails.application.routes.url_helpers.event_path(id), #:color => "red" } end def self.format_date(date_time) Time.at(date_time.to_i).to_formatted_s(:db) end 

是否有任何特定的方法来显示事件颜色根据他们的event_type意味着如果事件类型是学校它会显示红色

你打算在模特之外的其他地方使用这些颜色吗?

最有可能的是,您无需使其全局可用,因此只需向模型添加常量:

 scope :before, ... scope :after, ... EVENT_COLORS = { "School" => "#ff0000", "Holidays" => "#00ff00", ... } EVENT_COLORS.default = "#0000ff" #optional step, set default color for events ... :url => Rails.application.routes.url_helpers.event_path(id), :color => EVENT_COLORS[self.event_type]