我们有一个 rails 5.1.7
应用程序,并且在开发环境中一切正常。但在暂存环境中或使用 rails s -e staging
运行服务器时,我们会看到此错误“Sprockets::Rails::Helper::AssetNotFound”。
示例,资源“fontawesome-free-5.1.1-web/css/all.min.css”不存在于资源管道中
文件夹fontawesome-free-5.1.1-web
位于public/styleheets
下。
我尝试将文件夹移动到 vendor/stylesheets
或 assets/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
任何有关这里可能出现问题的帮助都会非常好,谢谢。
最佳答案
我假设你正在这样做:
<%= stylesheet_link_tag 'fontawesome/all.min.css' %>
vendor/
└── assets/ # <= this one is important
└── stylesheets/
└── fontawesome/
└── all.min.css
vendor/assets
和 app/assets
中的目录会自动添加到 Assets 路径中。这些是 sprocket 用于查找 Assets 的路径:
>> 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
,这意味着 sprocket 已完成在 Assets 路径中查找并动态编译。现在需要在 public/assets
中预编译 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
更新
要批量处理文件,有两种选择。添加一个可以动态匹配预编译资源的过程:
# 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
,您不希望在开发中使用预编译 Assets 。
关于ruby-on-rails - Rails 5.1.7:Sprockets::Rails::Helper::AssetNotFound,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/77048064/