如何将 ruby 变量传递给 JavaScript 函数。这里的 ruby 变量是一个活动的记录对象。
= label = @label // active record object
- @x = 5
= javascript_tag "alert(#{ j(@x.to_json)});" // Working
= javascript_tag "proj.app.custom(#{ j(label.to_json)})" // Not working
proj.app.custom是一个JavaScript函数。
proj.app.custom = function(param) {
};
我收到以下错误
Uncaught SyntaxError: Invalid or unexpected token
不确定label
包含什么,但如果你想传递一个 json,你不应该转义它:
= javascript_tag "proj.app.custom(#{label.to_json})"
为什么不直接做:
= javascript_tag "proj.app.custom(#{ j(@label.to_json)})"
我有一种感觉,你对上述= label = @label
的分配是无效的。