成熟的应用程序更新到 Rails 7.0.7、Ruby 3.1.2,使用安装程序添加
stimulus-rails
,已安装 importmap-rails
、turbo-rails
和 sprockets-rails
。还没有自定义刺激控制器,只是尝试“开箱即用”加载设置。
我在 StackOverflow 上阅读了十几个关于此问题的问答,包括 Alex 对
importmap-rails
工作原理的冗长而精彩的解释,但似乎没有一个能够解释这种情况。
当我在本地启动服务器时,我得到
ActionController::RoutingError (No route matches [GET] "/stimulus-loading.js"):
导入图中一切似乎都正确:
这是
./bin/importmap json
的结果:
{
"imports": {
"application": "/assets/application.js",
"@rails/ujs": "/assets/@rails--ujs.js",
"@hotwired/turbo-rails": "/assets/turbo.min.js",
"@hotwired/stimulus": "/assets/stimulus.min.js",
"@hotwired/stimulus-loading": "/assets/stimulus-loading.js",
"controllers/application": "/assets/controllers/application.js",
"controllers/hello_controller": "/assets/controllers/hello_controller.js",
"controllers": "/assets/controllers/index.js"
}
}
我已经按照几个答案中的建议运行了
rails assets:clobber
。这是标准设置,这些行都是由安装程序为 turbo-rails
、importmap-rails
和 stimulus-rails
宝石添加的。
# config/importmap.rb
pin "application", preload: true
pin "@rails/ujs", to: "@rails--ujs.js" # @7.0.7
pin "@hotwired/turbo-rails", to: "turbo.min.js", preload: true
pin "@hotwired/stimulus", to: "stimulus.min.js", preload: true
pin "@hotwired/stimulus-loading", to: "stimulus-loading.js", preload: true
pin_all_from "app/javascript/controllers", under: "controllers"
# application.js
import "@hotwired/turbo-rails"
import Rails from "@rails/ujs"
import "@hotwired/stimulus"
import "@hotwired/stimulus-loading"
Rails.start(); import "controllers"
这是我的
assets/config/manifest.js
//= link_tree ../images
//= link_tree ../javascripts .js
//= link_tree ../../javascript .js
//= link_tree ../../../vendor/javascript .js
//= link my_application.js
我重命名了
app/assets/javascripts/my_application.js
以避免与app/javascript/application.js
命名冲突
我已将
app/javascript
添加到资源路径中(似乎没有更改任何内容)
# config/initializers/assets.rb
Rails.application.config.assets.paths << Rails.root.join("app/javascript")
我添加了资产调试并尝试打开/关闭编译
# config/environments/development
config.assets.compile = true # if false, no assets found
config.assets.debug = true # didn't tell me anything new
开发模式下页面加载日志:
Started GET "/assets/es-module-shims.min.js" for 10.0.2.2 at 2023-09-06 16:47:39 +0000
10.0.2.2 - - [06/Sep/2023:16:47:38 UTC] "GET /assets/es-module-shims.min.js HTTP/1.1" 304 0
http://localhost:3000 -> /assets/es-module-shims.min.js
Started GET "/assets/BlackOnWhite.css" for 10.0.2.2 at 2023-09-06 16:47:40 +0000
Started GET "/assets/my_application.js" for 10.0.2.2 at 2023-09-06 16:47:41 +0000
10.0.2.2 - - [06/Sep/2023:16:47:38 UTC] "GET /assets/mo_application.js HTTP/1.1" 304 0
http://localhost:3000 -> /assets/mo_application.js
10.0.2.2 - - [06/Sep/2023:16:47:38 UTC] "GET /assets/BlackOnWhite.css HTTP/1.1" 304 0
http://localhost:3000 -> /assets/BlackOnWhite.css
Started GET "/assets/application.js" for 10.0.2.2 at 2023-09-06 16:47:42 +0000
10.0.2.2 - - [06/Sep/2023:16:47:42 UTC] "GET /assets/application.js HTTP/1.1" 200 367
http://localhost:3000 -> /assets/application.js
Started GET "/assets/stimulus.min.js" for 10.0.2.2 at 2023-09-06 16:47:44 +0000
Started GET "/assets/controllers/index.js" for 10.0.2.2 at 2023-09-06 16:47:44 +0000
Started GET "/assets/turbo.min.js" for 10.0.2.2 at 2023-09-06 16:47:45 +0000
10.0.2.2 - - [06/Sep/2023:16:47:43 UTC] "GET /assets/stimulus.min.js HTTP/1.1" 304 0
http://localhost:3000 -> /assets/stimulus.min.js
Started GET "/assets/stimulus-loading.js" for 10.0.2.2 at 2023-09-06 16:47:46 +0000
10.0.2.2 - - [06/Sep/2023:16:47:43 UTC] "GET /assets/turbo.min.js HTTP/1.1" 304 0
http://localhost:3000 -> /assets/turbo.min.js
ActionController::RoutingError (No route matches [GET] "/stimulus-loading.js"):
10.0.2.2 - - [06/Sep/2023:16:47:43 UTC] "GET /assets/stimulus-loading.js HTTP/1.1" 404 0
http://localhost:3000 -> /assets/stimulus-loading.js
10.0.2.2 - - [06/Sep/2023:16:47:43 UTC] "GET /assets/controllers/index.js HTTP/1.1" 304 0
http://localhost:3000-> /assets/controllers/index.js
从日志来看,
/assets/stimulus-loading.js
和/stimulus-loading.js
之间可能存在路径差异。但浏览器显示 404 为 http://localhost:3000/assets/stimulus-loading.js
。
解决了。
我在 stimulus-rails github 页面上检查问题,这是一个已知的当前问题,与 4.1.1 的 当前 Sprockets 问题相关,其中名称中的连字符为“stimulus-loading.js”使链轮跳过它,因为它认为它是摘要。
解决方案是在
config.assets.digest = true
中设置config/environments/development.rb
。根据链接的 Stimulus 问题,这是 Rails 团队首选的开发设置,但在我之前,它就在我的遗留应用程序中被关闭了。