我正在嘗試將我的應用程式從 Rails 5.2 更新到 6.1,并使用 Zeitwerk 自動加載,但我在自動加載/lib類時遇到了一些問題。
我將其包含在config/application.rb:
config.load_defaults 5.2
config.autoloader = :zeitwerk
config.enable_dependency_loading = true
config.autoload_paths = Dir[Rails.root.join('lib/**/*')]
config.eager_load_paths = Dir[Rails.root.join('lib/**/*')]
而且,當我運行時zeitwerk:check,它會提醒我:
Hold on, I am eager loading the application.
expected file lib/facades/coconut/v2/foo.rb to define constant Foo
它被宣告為:
# /lib/facades/coconut/v2/foo.rb
module Coconut
module V2
class Foo
# ...
end
end
end
當我嘗試除錯它(將 abinding.pry放入某個測驗檔案中)時,Zeitwerk 說它沒有定義,直到我在沒有模塊(命名空間)的情況下呼叫它:
[1] pry(#<#filename>)> Coconut::V2::Foo
NameError: uninitialized constant Coconut::V2::Foo
from (pry):1:in `setup`
[2] pry(#<#filename>)> Foo
Zeitwerk::NameError: expected file /my-app/lib/api/coconut/v2/foo.rb to define constant Foo, but didn't
from /usr/local/bundle/gems/zeitwerk-2.5.4/lib/zeitwerk/loader/callbacks.rb:25:in `on_file_autoloaded'
[3] pry(#<#filename>)> Coconut::V2::Foo
=> Coconut::V2::Foo
聽起來很奇怪,因為其中還有另一個類/lib遵循相同的結構,但效果很好,例如
# /lib/api/services/youtube/client.rb
module Services
module Youtube
class Client
# ...
end
end
end
binding.pry在一些測驗中檢查它:
pry(#<#filename>)> Services::Youtube::Client
=> Services::Youtube::Client
有沒有人遇到過這個問題或知道會發生什么?
系統:
- 紅寶石 2.7.6p219
- 導軌 6.1.6
- 時代周刊 (2.5.4)
uj5u.com熱心網友回復:
自動加載路徑是根目錄,而不是其內容。
您需要洗掉此處記錄的通配符。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/477395.html
標籤:轨道上的红宝石 红宝石 自动加载 ruby-on-rails-6.1 时代周刊
