【问题标题】:Unexpected 'LOGIC' in coffeescript template咖啡脚本模板中出现意外的“逻辑”
【发布时间】:2012-01-19 19:56:54
【问题描述】:

将此代码放在 .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 行的 &lt;%= 中删除 = 并在错误停止之前添加某种虚拟数组时:

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

【问题讨论】:

    标签: ruby-on-rails view ruby-on-rails-3.1 coffeescript


    【解决方案1】:

    在我的咖啡脚本文件中使用 to_json 将实例变量解析为 json 时,我遇到了同样的问题。

    我通过将html_safe 附加到该行解决了这个问题:

    @instance_var.to_json.html_safe

    【讨论】:

    • 我认为这是有道理的!有时间我会试试的,同时我使用的是更基本的方法......
    • 谢谢!很高兴我终于找到了 .html_safe
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-15
    • 2015-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-01
    • 2019-08-06
    相关资源
    最近更新 更多