我可能应该首先提到我没有预编译。
我有 8 个不同的 Js 文件(7 个,不包括 Application.js),当我使用 <%= javascript_include_tag 'application' %>
时它打印出:
<script src="/assets/admin.js?body=1" type="text/javascript"></script>
<script src="/assets/brand.js?body=1" type="text/javascript"></script>
<script src="/assets/category.js?body=1" type="text/javascript"></script>
<script src="/assets/home.js?body=1" type="text/javascript"></script>
<script src="/assets/product.js?body=1" type="text/javascript"></script>
<script src="/assets/setting.js?body=1" type="text/javascript"></script>
<script src="/assets/user.js?body=1" type="text/javascript"></script>
<script src="/assets/application.js?body=1" type="text/javascript"></script>
因此,我的一些 jQuery(使用 Toggles)无法工作,因为它们被执行了多次。
如何让它只使用 application.js?
我的Application.js文件:
// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// the compiled file.
//
// WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED, ANY BLANK LINE SHOULD
// GO AFTER THE REQUIRES BELOW.
//
//= require jquery
//= require jquery_ujs
//= require jquery.ui.all
//= require_tree .
最佳答案
除了删除 //= require_tree .
正如迈克所说。尝试以下命令:
$ rake tmp:clear tmp:create assets:clean
这将清除您的临时文件和缓存的资源文件。
此外,如果您只需要单个 application.js
而不是 7 个 .js
,请包含脚本标记。设置以下选项config/environments/development.rb
# Expands the lines which load the assets
config.assets.debug = false
希望对你有帮助
关于javascript - HTML 中包含的 JS 文件的 Ruby on Rails 副本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16611250/