【发布时间】:2016-12-18 02:49:01
【问题描述】:
首先让我告诉你,我已经搜索过这个问题并没有找到任何答案。
我的 layouts/application.html.haml 文件中有很多 Javascript:
### at the end of application.html.haml file
- if @instance_variable.condition
:javascript
// a lot js/jQuery code
:javascript
// more js code that uses #{ @instance.vars }
- if @another_condition.using.ruby.cody
:javascript
// more js code that uses #{ @instance.vars }
我在这段代码中使用了实例变量,这意味着这个变量只会在控制器(应用程序控制器)上声明。由于这是应用程序布局,因此这些脚本在我网站的每个页面上运行(这当然是我所需要的)。
我想要的是将所有这些代码移动到 js.erb 文件(或 .haml,如果可能的话)。首先 - 因为更容易在单独的文件中管理此代码;第二 - 因为我不想在每个 html 文件的末尾有 <script> // a lot of js code </script> 标签,我只想有一个 <script async src='link'/> 标签。
我也已经在文件的开头包含了application.coffee,但我需要在 html 文件的末尾使用这个脚本标签。
【问题讨论】:
标签: javascript jquery ruby-on-rails asset-pipeline erb