ruby-on-rails - 在生产环境中使用 Rails 7 中的 importmaps 时出现 404 错误
问题描述
我不确定这是 importmaps 问题还是其他问题,但在 Rails 7.0.0.alpha2 中,我在 javascript 文件上遇到 404 错误。
想知道我是否缺少某种生产“编译”步骤,因为它在开发中运行良好。
# app/javascript/application.js
import "@hotwired/turbo-rails"
import "controllers"
# app/javascript/controllers/index.js
import { application } from "./application"
import VoteController from "./vote_controller.js"
application.register("vote", VoteController)
# app/javascript/controllers/vote_controller.js
import { Controller } from "@hotwired/stimulus"
// Connects to data-controller="vote"
export default class extends Controller {
static targets = ["element"];
toggle(event) {
//event.preventDefault();
event.target.classList.add("opacity-100");
event.target.classList.remove("opacity-0");
}
}
# config/importmap.rb
pin "application", preload: true
pin "@hotwired/turbo-rails", to: "turbo.js"
pin "@hotwired/stimulus", to: "stimulus.js"
pin "@hotwired/stimulus-loading", to: "stimulus-loading.js"
pin_all_from "app/javascript/controllers", under: "controllers"
然后在我的app/views/layouts/application.html.erb
文件中,我<%= javascript_importmap_tags %>
用来包含所有内容。
如果我设置config.assets.compile = true
,production.rb
错误就会消失......但我不确定为什么或是否解决了核心问题。
解决方案
Rails 7.0.0app/javascript/controllers/index.js
已修改。我找到了几种不同的方法来解决这个问题。
首先尝试将您的import { application }
行更改为 import from controllers/application
,如下所示:
import { application } from "controllers/application"
然后修改每个特定控制器导入的from
参数,如下所示"controllers/name_controller"
:
可选:
删除每个控制器的单独导入并使用:
// Eager load all controllers defined in the import map under controllers/**/*_controller
import { eagerLoadControllersFrom } from "@hotwired/stimulus-loading"
eagerLoadControllersFrom("controllers", application)
或这个:
// Lazy load controllers as they appear in the DOM (remember not to preload controllers in import map!)
import { lazyLoadControllersFrom } from "@hotwired/stimulus-loading"
lazyLoadControllersFrom("controllers", application)
这似乎已经为我解决了。奇怪的是,该rails stimulus:manifest:update
命令将用不起作用的旧样式替换它。
有关根本原因的更多信息和讨论: https ://github.com/hotwired/stimulus-rails/issues/87
推荐阅读
- unity3d - 从 AWS 服务器上传/下载 IFC 模型到我们的 Unity 应用程序
- pagespeed - The same indicators of the PAGE SPEED API
- php - Symfony 4.4。捆绑安装后运行迁移
- python - Hypothesis.strategies 从日期生成字符串
- css - 如何自定义引导 css
- python - 如何清理 python 中的标准输入以修复 sonarqube 问题
- python - 每 2 秒运行一次 python
- junit - 赛普拉斯 JUnit 报告:缺少“跳过”和“错误”属性
- .net-core - SymmetricSecurityKey system.ArgumentNullException 参数“s”
- python - NameError Traceback(最近一次调用最后一次)