[英]How can I conditionally include some Javascript in an html.slim file, as part of a larger Ruby on Rails project?
I want my page to render some Javascript, conditional on a variable from the Rails environment.我希望我的页面呈现一些 Javascript,以 Rails 环境中的变量为条件。 I must have everything in this one file, because of other build tooling constraints.
由于其他构建工具的限制,我必须在这个文件中包含所有内容。
Eseentially I am trying to get this working:本质上,我试图让这个工作:
= javascript:
console.log('My javascript goes in this area....');
<% if myCondition %>
console.log('Render the JS in here is myCondition is true')
<%end>
However, when I try this, I get errors that <
is not a valid character.但是,当我尝试此操作时,我收到
<
不是有效字符的错误。
javascript:
console.log('My javascript goes in this area....');
- if myCondition
javascript:
console.log('Render the JS in here is myCondition is true')
Also, this question might be useful: ruby inside javascript block [slim template]此外,这个问题可能很有用: javascript 块内的 ruby [slim template]
you cannot use <%... %>
in your javascript您不能在 javascript 中使用
<%... %>
If you want to use rails data, try ei如果你想使用 rails 数据,试试 ei
var form_id = "form"+ #{{@product.id.to_json}};
Another way, you should be use ajax( it was mean your_action.js.slim ), see ie bellow另一种方式,你应该使用 ajax(意思是 your_action.js.slim ),见下面
create.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.