2011-12-13 213 views
1

具有局部型.js.coffee的視圖這個代碼(它是被呈現爲一個視圖,而不是作爲一種資產):「邏輯」

<% if @followed_car_ids.present? %> 
for car_id_and_path in <%= @followed_car_ids_and_paths.to_json %> 
    toggle_follow(car_id_and_path[0], true, car_id_and_path[1]) 
<% end %> 

在控制器:

@followed_car_ids_and_paths = @followed_cars.map{|car| [car.id, url_for(current_user.car_subscriptions.find_by_car_id(car))]} 

注意,這並不重要,如果我把地圖(或收集)在視圖中。無法正常工作或如果我把一個簡單的字符串,而不是url_for(...。錯誤停止前

ActionView::Template::Error (Error: Parse error on line 1: Unexpected 'LOGIC'): 
    1: <% if @followed_car_ids.present? %> 
    2: for car_id_and_path in <%= @followed_car_ids_and_paths.to_json %> 
    3: toggle_follow(car_id_and_path[0], true, car_id_and_path[1]) 
    4: <% end %> 
    app/views/general_ajax/_update_followed_cars.js.coffee:1:in `_app_views_general_ajax__update_followed_cars_js_coffee__3478461849674996439_70355260673980' 

當我從<%=刪除=第2行,並添加某種僞排列的:

for car_id_and_path in [1,2,3] <% @followed_car_ids_and_paths.to_json %> 

誤差不

它給這個錯誤如果我使用平面陣列(不是地圖的兩倍),則會發生:

for car_id_and_path in <%= @followed_car_ids %> 

無論是當它是這樣的:

for car_id_and_path in [[1, 'a'],[2, 'b'],[3, 'c']] 

回答

4

我有同樣的問題與解析實例變量在我的CoffeeScript文件中使用to_json到JSON。

我通過附加html_safe到線解決了這個問題:

@instance_var.to_json.html_safe

+0

我覺得這是有道理的!我來試試,當我得到的時候,同時我使用的是更基本的方法...... – Cristian 2012-08-28 07:50:46