Rails 资产管道不加载 javascript 依赖项
Rails asset pipeline doesn't load javascript dependencies
我一直在关注 Rails 教程中 Michael Hartl 的 Ruby,但在第 8 章中加载 JavaScript 依赖项时遇到问题:https://www.railstutorial.org/book/basic_login
CSS 依赖项加载良好(我正在使用 bootstrap),在浏览器中检查页面时,头部有多个 <link rel="stylesheet...>
标记。但是,只有一个脚本标签,它加载编译后的 application.js
文件。
有趣的是,即使我在 js 清单文件中出错,例如需要一个不存在的文件,在浏览器中呈现我的页面时也不会出现任何错误。
我试过在一个新的应用程序中复制它,但我做不到。只需在 Gemfile bundle install
中请求所需的 gem 并访问除默认 rails 页面之外的任何页面,将包括 application.js 清单文件中列出的所有依赖项作为单独的 <script src=..>
标签在 <head>
。
我正在加载资产作为 layouts/application.html.erb
文件的一部分,使用助手:
<%= csrf_meta_tags %>
<%= stylesheet_link_tag 'application', media: 'all',
'data-turbolinks-track': 'reload' %>
<%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>
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 any plugin's vendor/assets/javascripts directory 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
// compiled file. JavaScript code in this file should be added after the last require_* statement.
//
// Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
// about supported directives.
//
//= require jquery
//= require jquery_ujs
//= require bootstrap
//= require turbolinks
//= require_tree .
我的RoR和Ruby版本分别是5.1和2.3.2。
js清单文件是app/assets/javascripts/application.js
我通过将我的主应用程序的相关部分移动到一个新的 rails 应用程序来解决这个问题,该应用程序确实加载了 JS 依赖项。我希望这是我这边的配置问题。
更具体地说,我将 Gemfile、db/、config/routes.rb 和 app/(config/ 除外)中的所有内容移动到新的 rails 应用程序中。一切正常后,我用新应用程序中的目录覆盖了主应用程序的 app/ 目录。
我一直在关注 Rails 教程中 Michael Hartl 的 Ruby,但在第 8 章中加载 JavaScript 依赖项时遇到问题:https://www.railstutorial.org/book/basic_login
CSS 依赖项加载良好(我正在使用 bootstrap),在浏览器中检查页面时,头部有多个 <link rel="stylesheet...>
标记。但是,只有一个脚本标签,它加载编译后的 application.js
文件。
有趣的是,即使我在 js 清单文件中出错,例如需要一个不存在的文件,在浏览器中呈现我的页面时也不会出现任何错误。
我试过在一个新的应用程序中复制它,但我做不到。只需在 Gemfile bundle install
中请求所需的 gem 并访问除默认 rails 页面之外的任何页面,将包括 application.js 清单文件中列出的所有依赖项作为单独的 <script src=..>
标签在 <head>
。
我正在加载资产作为 layouts/application.html.erb
文件的一部分,使用助手:
<%= csrf_meta_tags %>
<%= stylesheet_link_tag 'application', media: 'all',
'data-turbolinks-track': 'reload' %>
<%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>
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 any plugin's vendor/assets/javascripts directory 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
// compiled file. JavaScript code in this file should be added after the last require_* statement.
//
// Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
// about supported directives.
//
//= require jquery
//= require jquery_ujs
//= require bootstrap
//= require turbolinks
//= require_tree .
我的RoR和Ruby版本分别是5.1和2.3.2。
js清单文件是app/assets/javascripts/application.js
我通过将我的主应用程序的相关部分移动到一个新的 rails 应用程序来解决这个问题,该应用程序确实加载了 JS 依赖项。我希望这是我这边的配置问题。
更具体地说,我将 Gemfile、db/、config/routes.rb 和 app/(config/ 除外)中的所有内容移动到新的 rails 应用程序中。一切正常后,我用新应用程序中的目录覆盖了主应用程序的 app/ 目录。