无法使用 Rails 6 在生产环境中加载资产资源(css + js)

1 css ruby-on-rails webpacker ruby-on-rails-6

当我在生产模式下启动应用程序时,资产(css + js)无法正确加载。

我进入浏览器控制台net::ERR_ABORTED 404 (Not Found)或 Rails 日志ActionController::RoutingError (No route matches [GET] "/packs/js/application-51bdb73d26ac893c3407.js"):

我制作rake assets:precompile并提交config.assets.compile = true了environments/Production.rb,但结果相同

对于生产模式,我使用相同的应用程序和相同的目录,我只是将其-e production作为参数添加到rails server命令中

Fiz*_*lad 6

看来你应该将RAILS_SERVE_STATIC_FILES环境变量设置为true

建议基于我在解决相同问题时发现的此问题评论

这仅影响以下位置的配置config/environments/production.rb

  # Disable serving static files from the `/public` folder by default since
  # Apache or NGINX already handles this.
  config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present?
Run Code Online (Sandbox Code Playgroud)