我们有一个
rails 5.1.7
应用程序,并且在开发环境中一切正常。但在临时环境中或使用 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/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
中的目录会自动添加到资产路径中。这些是 sprocket 用于查找资产的路径:
>> 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 已完成在资源路径中查找并动态编译。现在预计预编译资源位于 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