0

我试图通过我的rails控制器从我的数据库中返回一组日期,然后在渲染日历时由Javascript使用。当我拉起rails控制台进行测试但不在视图中时,它似乎正在工作。有任何想法吗?我的代码如下。

装备 has_many line_items

LineItem 属于_to Gear

Javascript 变量

var $myBadDates = new Array("<%= @gear.line_items.rented %>");

正在返回的视图。

var $myBadDates = new Array("[]");

订单项模型(缩短)

class LineItem < ActiveRecord::Base
  belongs_to :gear

  scope :available, where(:cart_id => nil)

  def self.rented
    LineItem.available.collect {|x| (x.rentstart..x.rentend).to_a}
  end

end

来自 Rails 控制台的数组

1.9.3-p194 :007 > g.line_items.rented
  LineItem Load (0.7ms)  SELECT `line_items`.* FROM `line_items` WHERE `line_items`.`gear_id` = 4 AND `line_items`.`cart_id` IS NULL
 => [[Tue, 12 Feb 2013, Wed, 13 Feb 2013, Thu, 14 Feb 2013, Fri, 15 Feb 2013, Sat, 16 Feb 2013, Sun, 17 Feb 2013, Mon, 18 Feb 2013, Tue, 19 Feb 2013, Wed, 20 Feb 2013, Thu, 21 Feb 2013], [Tue, 05 Feb 2013, Wed, 06 Feb 2013, Thu, 07 Feb 2013, Fri, 08 Feb 2013, Sat, 09 Feb 2013, Sun, 10 Feb 2013, Mon, 11 Feb 2013, Tue, 12 Feb 2013, Wed, 13 Feb 2013, Thu, 14 Feb 2013, Fri, 15 Feb 2013]] 

已接受答案的更新工作 javascript 代码

var $myBadDates = <%= @gear.line_items.rented.flatten.to_json.html_safe %>;

4

1 回答 1

2

尝试使用to_json

var $myBadDates = <%= @gear.line_items.rented.to_json %>;
于 2013-03-07T05:42:42.857 回答