gpt4 book ai didi

ruby-on-rails - rails 5 : Autoloading with custom folders not working

转载 作者:数据小太阳 更新时间:2023-10-29 07:07:43 25 4
gpt4 key购买 nike

我有这门课:

# app/events/new_request.rb
class Events::NewRequest
end

然后我将该文件夹添加到自动加载:

# config/application.rb
config.autoload_paths += %W( events/ )

当运行 rails c 时:

> Events::NewRequest
NameError: uninitialized constant Events

问题是,如果我在定义类时不使用命名空间“Events”,它会成功自动加载类。

最佳答案

module Sandbox
class Application < Rails::Application
config.autoload_paths += [config.root.join('app')]
end
end

这将使 Rails 从 app/events/new_request.rb 自动加载 Events::NewRequest

irb(main):001:0> Events
=> Events
irb(main):002:0> Events::NewRequest
=> Events::NewRequest

关于ruby-on-rails - rails 5 : Autoloading with custom folders not working,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43689432/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com