ruby Rails 5.1.7:Sprockets::Rails::Helper::AssetNotFound

t98cgbkg  于 2023-10-17  发布在  Ruby
关注(0)|答案(1)|浏览(132)

我们有一个rails 5.1.7应用程序,在开发环境中一切正常。但是在staging环境中或者当使用rails s -e staging运行服务器时,我们看到这个错误“Sprockets::Rails::Helper::AssetNotFound”。
例如,The asset "fontawesome-free-5.1.1-web/css/all.min.css" is not present in the asset pipeline
文件夹fontawesome-free-5.1.1-web位于public/styleheets下。
我已经尝试将文件夹移动到vendor/stylesheetsassets/stylesheets,但我看到相同的错误。
下面是我的environments/staging.rb

AppName::Application.configure do
  config.cache_classes = true

  config.consider_all_requests_local       = true
  config.action_controller.perform_caching = true
  config.action_view.cache_template_loading = true
  config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect'

  config.log_level = :debug
  memcached_config = YAML.load_file(Rails.root.join('config/memcached.yml'))
  memcached_hosts = memcached_config['defaults']['servers']
  config.cache_store = :mem_cache_store, *memcached_hosts

  config.action_mailer.delivery_method = :smtp
  config.action_mailer.perform_deliveries = true
  config.action_mailer.raise_delivery_errors = true
  config.i18n.fallbacks = true

  config.active_support.deprecation = :notify

  config.eager_load = true
  config.assets.js_compressor = Uglifier.new(harmony: true)
  config.assets.compile = false
  config.assets.raise_runtime_errors = true
  config.assets.debug = true
end

任何关于这里可能出错的帮助都将是非常好的,谢谢。

3qpi33ja

3qpi33ja1#

我猜你是这么做的

<%= stylesheet_link_tag 'fontawesome/all.min.css' %>
vendor/
└── assets/                  # <= this one is important
    └── stylesheets/
        └── fontawesome/
            └── all.min.css

vendor/assetsapp/assets中的目录会自动添加到资产路径中。以下是Oracle正在使用的查找资产的路径:

>> puts Rails.application.config.assets.paths
...
/myapp/app/assets/stylesheets
/myapp/vendor/assets/stylesheets         # <=
/usr/local/bundle/gems/coffee-rails-4.2.2/lib/assets/javascripts
/usr/local/bundle/gems/actioncable-5.2.8.1/lib/assets/compiled
...

你有config.assets.compile = false,这意味着MySQL已经完成了在资产路径中的查找和动态编译。它现在期待public/assets中的预编译资产:

# assuming sprockets is still serving files (but not compiling them anymore)
>> Rails.application.config.public_file_server.enabled
=> true
>> Rails.application.config.assets.compile
=> false

>> helper.asset_path("fontawesome/all.min.css")
Traceback (most recent call last):
        1: from (irb):5
Sprockets::Rails::Helper::AssetNotFound (The asset "fontawesome/all.min.css" is not present in the asset pipeline.)

你必须bin/rails assets:precompile

# config/initializers/assets.rb

# but make it precompilable first
Rails.application.config.assets.precompile += ["fontawesome/all.min.css"]
$ bin/rails assets:precompile

I, [2023-09-10T10:04:24.038220 #1]  INFO -- : Writing /myapp/public/assets/fontawesome/all.min-1f62e82d4d0217052a8d48596d631f5c58ee5149386c719419046118e4fa43f3.css
...

现在应该工作:

>> helper.asset_path("fontawesome/all.min.css")
=> "/assets/fontawesome/all.min-1f62e82d4d0217052a8d48596d631f5c58ee5149386c719419046118e4fa43f3.css"

# check if it is being served
>> require "open-uri"
>> URI.open(helper.asset_path("fontawesome/all.min.css", host: "http://localhost:3000")).read
=> "i am font\n"
  • https:guides.rubyonrails.org/v5.0/configuring.html#configuring-assets*
    更新

要批量处理文件,有两种选择。添加一个proc,它将动态匹配用于预编译的资产:

# config/initializers/assets.rb

VENDOR_ASSETS = lambda do |logical_path, filename|
  filename.start_with?(::Rails.root.join("vendor/assets/").to_s) &&
    [".css", ".js"].include?(File.extname(logical_path))
end

Rails.application.config.assets.precompile += [VENDOR_ASSETS]
  • https:github.com/rails/sprockets-rails/blob/v3.4.2/lib/sprockets/railtie.rb#L88*

一个更好的选择是使用manifest.js,这是后来的rails版本中的默认值:

# config/initializers/assets.rb

# don't need this in sprockets v4
Rails.application.config.assets.precompile += ["manifest.js"]
// app/assets/config/manifest.js

//= link_tree ../images
//= link_directory ../javascripts .js
//= link_directory ../stylesheets .css
//
// link everything to be precompiled
//= link_tree ../../../vendor/assets/stylesheets .css
//= link_tree ../../../vendor/assets/javascripts .js
//
// or one file at a time 
//= link fontawesome/all.min.css
  • https:github.com/rails/sprockets#directives*

另外,当你预编译资源时,这意味着你想直接通过url加载它们。如果你只使用//= require指令,那么所需的文件不需要被预编译,因为它们被合并到被预编译的文件中。你只会得到一个大的application.css文件:

/* app/assets/stylesheets/application.css */

/*
 *= require_tree .
 *= require_self
 *= require fontawesome/all.min
 */

如果你在本地进行预编译来测试,不要忘记在完成后使用bin/rails assets:clobber,你不希望在开发中使用预编译的资产。

相关问题