[英]How can I conditionally include some Javascript in an html.slim file, as part of a larger Ruby on Rails project?
我希望我的页面呈现一些 Javascript,以 Rails 环境中的变量为条件。 由于其他构建工具的限制,我必须在这个文件中包含所有内容。
本质上,我试图让这个工作:
= javascript:
console.log('My javascript goes in this area....');
<% if myCondition %>
console.log('Render the JS in here is myCondition is true')
<%end>
但是,当我尝试此操作时,我收到<
不是有效字符的错误。
javascript:
console.log('My javascript goes in this area....');
- if myCondition
javascript:
console.log('Render the JS in here is myCondition is true')
此外,这个问题可能很有用: javascript 块内的 ruby [slim template]
您不能在 javascript 中使用<%... %>
如果你想使用 rails 数据,试试 ei
var form_id = "form"+ #{{@product.id.to_json}};
另一种方式,你应该使用 ajax(意思是 your_action.js.slim ),见下面
创建.js.slim
- if @product.errors.present?
| toastr["error"]("#{@product.errors.full_messages[0]}");
- else
| toastr["success"]("Created successfully!");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.