“逻辑”

问题描述:

具有局部型.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']] 

我有同样的问题与解析实例变量在我的CoffeeScript文件中使用to_json到JSON。

我通过附加html_safe到线解决了这个问题:

@instance_var.to_json.html_safe

+0

我觉得这是有道理的!我来试试,当我得到的时候,同时我使用的是更基本的方法...... – Cristian 2012-08-28 07:50:46