我不确定这是 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
错误就会消失......但我不确定为什么或是否解决了核心问题。