javascript - HTML 中包含的 JS 文件的 Ruby on Rails 副本

标签 javascript ruby-on-rails asset-pipeline

我可能应该首先提到我没有预编译。

我有 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/

相关文章:

javascript - 将视频控件添加到 CreateJS/EaselJS Canvas

javascript - 使用 Sanctuary 从具有特定 RecordType 的对象中选取字段

ruby-on-rails - Rails Controller 丢失 JSON 字符串中的换行符

css - 在 Rails 的 Assets 管道中将图像编码为 css

css - 为什么无法从 Rails 中的 View 加载管道内的图像?

ruby-on-rails - Rails Assets 管道中的动态 CSS,即时编译

javascript - Webpack 'vue-loader' 编译问题与 '@vue/compiler-sfc'

javascript - 回复评论 YouTube 风格

ruby-on-rails - Brew 更新中断 openssl

ruby-on-rails - 为什么这个 Rails Controller 测试失败了?