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
 => [[2013年2月12日, 2013年2月13日, 2013年2月14日, 2013年2月15日, 2013年2月16日, 2013年2月17日, 2013年2月18日, 2013年2月19日, 2013年2月20日, 2013年2月21日], [2013年2月05日, 2013年2月06日, 2013年2月07日, 2013年2月08日, 2013年2月09日, 2013年2月10日, 2013年2月11日, 2013年2月12日, 2013年2月13日, 2013年2月14日, 2013年2月15日]] 

UPDATED Working javascript code from accepted answer

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

asked Mar 7, 2013 at 5:39
1
  • I think @gear.line_items will return list of line_items not single line_item. So @ gear.line_items.rented wo'nt work. Commented Mar 7, 2013 at 5:44

1 Answer 1

2

try using to_json

var $myBadDates = <%= @gear.line_items.rented.to_json %>;
answered Mar 7, 2013 at 5:42
Sign up to request clarification or add additional context in comments.

6 Comments

That pulled the data but getting strange return values new Array("[[&quot;2013年02月12日&quot;,&quot;2013年02月13日&quot;,&quot;2013年02月14日&quot;,&quot;2013年02月15日&quot;,&quot;2013年02月16日&quot;,&quot;2013年02月17日&quot;,&quot;2013年02月18日&quot;,&quot;2013年02月19日&quot;,&quot;2013年02月20日&quot;,&quot;2013年02月21日&quot;],[&quot;2013年02月05日&quot;,&quot;2013年02月06日&quot;,&quot;2013年02月07日&quot;,&quot;2013年02月08日&quot;,&quot;2013年02月09日&quot;,&quot;2013年02月10日&quot;,&quot;2013年02月11日&quot;,&quot;2013年02月12日&quot;,&quot;2013年02月13日&quot;,&quot;2013年02月14日&quot;,&quot;2013年02月15日&quot;]]")
@DaveG: Perhaps tacking a .html_safe on the end would help.
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).
Is to_json vulnerable to injecting </script> 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".
jvnil & @muistooshort Thanks got it working from your suggestions.
|

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.