0

I'm trying to return an array of dates from my database through my rails controller, which is then used by Javascript while rendering a calendar. It seems to be working when I pull up the rails console for testing but not in the view. Any ideas? My code is below.

Gear has_many line_items

LineItem belongs_to Gear

Javascript Variable

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

View that is being returned.

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

Line Item Model (shortened)

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

Array from Rails Console

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]] 

UPDATED Working javascript code from accepted answer

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

DaveG
  • 1,203
  • 1
  • 25
  • 45
  • I think @gear.line_items will return list of line_items not single line_item. So @ gear.line_items.rented wo'nt work. – Sachin R Mar 07 '13 at 05:44

1 Answers1

2

try using to_json

var $myBadDates = <%= @gear.line_items.rented.to_json %>;
jvnill
  • 29,479
  • 4
  • 83
  • 86
  • That pulled the data but getting strange return values new Array("[["2013-02-12","2013-02-13","2013-02-14","2013-02-15","2013-02-16","2013-02-17","2013-02-18","2013-02-19","2013-02-20","2013-02-21"],["2013-02-05","2013-02-06","2013-02-07","2013-02-08","2013-02-09","2013-02-10","2013-02-11","2013-02-12","2013-02-13","2013-02-14","2013-02-15"]]") – DaveG Mar 07 '13 at 05:44
  • 1
    @DaveG: Perhaps tacking a `.html_safe` on the end would help. – mu is too short Mar 07 '13 at 05:48
  • 2
    Looks like you just stuck a to_json onto the end of your existing code. If you look at jvnill's response, that's not quite what he suggested. You're getting a json array as a response, quoting it, and making an array of 1 item (the quoted json response). – railsdog Mar 07 '13 at 05:49
  • 1
    Is `to_json` vulnerable to injecting `` or `]]>` in the data? Some implementations, like that found in PHP will prevent it by using appropriate escapes, but if it is not done, it's not suitable for JavaScript value injection even though "it will almost always work". –  Mar 07 '13 at 05:55
  • jvnil & @muistooshort Thanks got it working from your suggestions. – DaveG Mar 07 '13 at 06:01
  • @DaveG Apparently it *is* vulnerable, see http://stackoverflow.com/questions/7205902/how-to-safely-embed-json-in-html-document to ensure it is "safe". –  Mar 07 '13 at 07:43