单一投标行动的多个json输出

时间:2014-02-28 20:07:30

标签: ruby-on-rails json ruby-on-rails-3 fullcalendar

我有控制器操作,我希望json输出为fullcalendar提供一系列变量。

as_json方法的模型如下

def as_json(options = {})
  ((self.start_at).to_date..(self.end_at).to_date).each do |m|
   {
     :id => self.id,
     :title => self.subject,
     :description => self.category.category_name || "",
     :start => (self.start_at.to_datetime + i++).rfc822,
     :end => ((self.duration.minutes.since self.start_at).to_datetime + i++).rfc822,
     :allDay => false,#self.all_day,
     :recurring => false,
     :url => Rails.application.routes.url_helpers.conference_path(id),
     #:color => "red"
   } 
  end
end 

但它会抛出json_encode错误。

我也尝试在控制器中循环以呈现具有不同值的多个json,但是只能创建一个json响应。如何才能实现多个json响应的目标?

当前Json回复

[{"id":17,"title":"ki mas","description":"Rails","start":"Wed, 2 Apr 2014 01:00:00 +0000","end":"Wed, 2 Apr 2014 02:00:00 +0000","allDay":false,"recurring":false,"url":"/conferences/17"}]

但我需要这样的形式一个响应或任何东西来实现以下输出

[{"id":17,"title":"ki mas","description":"Rails","start":"Wed, 2 Apr 2014 01:00:00 +0000","end":"Wed, 2 Apr 2014 02:00:00 +0000","allDay":false,"recurring":false,"url":"/conferences/17"}]
[{"id":17,"title":"ki mas","description":"Rails","start":"Thurs, 3 Apr 2014 01:00:00 +0000","end":"Thurs, 3 Apr 2014 02:00:00 +0000","allDay":false,"recurring":false,"url":"/conferences/17"}]
[{"id":17,"title":"ki mas","description":"Rails","start":"Fri, 4 Apr 2014 01:00:00 +0000","end":"Fri, 4 Apr 2014 02:00:00 +0000","allDay":false,"recurring":false,"url":"/conferences/17"}]
[{"id":17,"title":"ki mas","description":"Rails","start":"Sat, 5 Apr 2014 01:00:00 +0000","end":"Sat, 5 Apr 2014 02:00:00 +0000","allDay":false,"recurring":false,"url":"/conferences/17"}]
....

等等.. 请帮我实现这个目标。

1 个答案:

答案 0 :(得分:0)

简单...... :)你只需管理服务器端输出这种格式......

[{"id":17,"title":"ki mas","description":"Rails","start":"Wed, 2 Apr 2014 01:00:00  +0000","end":"Wed, 2 Apr 2014 02:00:00 +0000","allDay":false,"recurring":false,"url":"/conferences/17"},
{"id":17,"title":"ki mas","description":"Rails","start":"Thurs, 3 Apr 2014 01:00:00 +0000","end":"Thurs, 3 Apr 2014 02:00:00 +0000","allDay":false,"recurring":false,"url":"/conferences/17"},
{"id":17,"title":"ki mas","description":"Rails","start":"Fri, 4 Apr 2014 01:00:00 +0000","end":"Fri, 4 Apr 2014 02:00:00 +0000","allDay":false,"recurring":false,"url":"/conferences/17"},
{"id":17,"title":"ki mas","description":"Rails","start":"Sat, 5 Apr 2014 01:00:00      +0000","end":"Sat, 5 Apr 2014 02:00:00      +0000","allDay":false,"recurring":false,"url":"/conferences/17"}]