Rails 5 生产环境中的资源未加载

5

我最近更新了Rails应用程序中的一些软件包,现在我的资产没有被提供。相反,我得到以下错误:

Failed to load resource: the server responded with a status of 404 (Not Found)

我的资产已经预编译:

assets.rb

# Be sure to restart your server when you modify this file.

# Version of your assets, change this if you want to expire all your assets.
Rails.application.config.assets.version = '1.0'

# Add additional assets to the asset load path
# Rails.application.config.assets.paths << Emoji.images_path

# Precompile additional assets.
# application.js, application.css, and all non-JS/CSS in app/assets folder are already added.
# Rails.application.config.assets.precompile += %w( search.js )
Rails.application.config.assets.precompile += %w(ckeditor/*)
Rails.application.config.assets.precompile += %w(ckeditor/config.js)
Rails.application.config.assets.precompile += %w( *.js ^[^_]*.css *.css.erb )

application.rb

require_relative 'boot'

require 'rails/all'

# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)


module DeployTest
  class Application < Rails::Application
    # Settings in config/environments/* take precedence over those specified here.
    # Application configuration should go into files in config/initializers
    # -- all .rb files in that directory are automatically loaded.
    config.assets.precompile += Ckeditor.assets
    config.assets.precompile += %w( ckeditor/* )
    config.autoload_paths += %W(#{config.root}/app/models/ckeditor)
    config.active_record.default_timezone = :local
    config.time_zone = 'Eastern Time (US & Canada)'
  end
end

在让我开启编译资产之前,请了解这是一个糟糕的想法。感谢任何建议。

更新:我通过添加以下内容使其工作:

  config.assets.digest = true

添加到我的config/environments/staging.rb文件中。奇怪的是之前我不需要它。


1
你升级了什么?你能使用git bisect找到导致问题的软件包并在这里发布吗? - Anthony
是的,我的意思是我进行了一个捆绑包升级,它基本上更新了一堆软件包。你可以在这里查看列表:https://github.com/NicholasLYang/blog/commit/0fe7ef4b4408eb4283bef9d7f74f9c13a09adf4d - Nicholas Yang
只是提醒一下,您永远不想在Gemfile中“bundle update”所有内容。通常您一次只更新一个gem,在此之前,您需要阅读发布说明,以了解任何需要关注的重大更改。 - Anthony
我会检查public/assets/application.css是否存在。如果不存在,则您可能没有正确编译资产,或者您正在寻找错误的文件。后者更有可能,因为如果您已经打开了digest选项(应该这样做,并且在生产中默认打开),路径将不是application.css。它将是一些随机名称,例如application-0f9j48f90490fj3409.css。请查看您的public/assets文件夹。生成的HTML将链接到摘要文件名,因此您缺少的application.css是预期的,而不是问题。只需使用标准的Rails标记从HTML链接到资产即可。 - mahemoff
好的,有趣。你说得对,application.css不存在,因为digest选项已经打开了。然而,我正在使用标准的Rails标签来链接资产:<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %> - Nicholas Yang
显示剩余3条评论
1个回答

3

有时您需要将这两个配置都添加到staging.rb或您希望更改反映在其中的任何环境中。

config.assets.compile = true #set to false by default
config.assets.digest = true 

4
不建议在生产环境中将编译选项设置为true。 - Kartikey Tanna
1
这会暴露你的应用程序中的一个重大漏洞。不要这样做 - jakenberg

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接