Rails - 为什么 RAILS_ROOT/lib 内的模型在生产模式下不可用?

发布于 2024-09-02 14:12:23 字数 395 浏览 2 评论 0原文

我有一个位于 RAILS_ROOT/lib 文件夹内的类,我在我的一个助手中使用它,并且它在开发中效果很好。

当我切换到生产环境时,应用程序抛出一个 NameError (uninitializedconstant SomeHelper::SomeClass),我必须在助手中手动加载它:

load "#{Rails.root}/lib/some_class.rb"

module SomeHelper
  def some_method
    sc = SomeClass.new
    # blah
  end
end

我的印象是 RAILS_ROOT/lib/ 中的所有内容* 应该对应用程序可用 - 我需要配置什么才能在生产模式下发生这种情况吗?谢谢。

I have a class located inside RAILS_ROOT/lib folder, which I use in one of my helpers, and it works great in development.

When I switch to production, the application throws a NameError (uninitialized constant SomeHelper::SomeClass), and I have to load it manually in the helper:

load "#{Rails.root}/lib/some_class.rb"

module SomeHelper
  def some_method
    sc = SomeClass.new
    # blah
  end
end

I was under the impression that everything inside RAILS_ROOT/lib/* should be available all to the app - is there anything I need to configure to make this happen in prod mode? thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

彻夜缠绵 2024-09-09 14:12:23

当你调用 SomeHelper::SomeClass 时,Rails 的自动加载机制将尝试加载 lib/some_helper/some_class.rb 处的文件。Rails

不会加载 lib/* 中的所有内容,它只会在发生 ConstMissing 时尝试加载文件。

When you call SomeHelper::SomeClass, Rails' autoloading mechanism will try to load file at lib/some_helper/some_class.rb

Rails won't load everything in lib/*, it will only try to load files when ConstMissing occurs.

孤檠 2024-09-09 14:12:23

您可能需要检查开发环境和生产环境之间的配置设置之间的差异:
config/environments/development.rb 和 config/environments/development.rb。

在 Rails 初始化例程中,会调用 load_plugins() 来加载 config.plugin_paths 中的所有插件。您需要确保包含您的文件夹 lib/ ,例如

config.plugin_paths = ["#{RAILS_ROOT}/lib/plugins", "#{RAILS_ROOT}/vendor/plugins"]

除了config.plugin_paths之外,您还可以命名应该命名的插件加载到config.plugins中。如果该变量包含 :all 则将加载所有(找到的)插件。

(顺便说一句:与任一环境相同的配置设置应位于 config/environment.rb 中。环境之间的任何差异均取决于相应 .rb 文件中的设置。)

You might need to check differences between the configuration settings between development and production environment:
config/environments/production.rb and config/environments/development.rb.

During the Rails initialization routine, load_plugins() is called which loads all plugins in config.plugin_paths. You need to make sure that your folder lib/ is included, like in

config.plugin_paths = ["#{RAILS_ROOT}/lib/plugins", "#{RAILS_ROOT}/vendor/plugins"]

In addition to config.plugin_paths, you can also name the plugins that should be loaded in config.plugins. If that variable contains :all then all plugins (found) will be loaded.

(By the way: configuration settings equal to either environment should go in config/environment.rb. Any differences between enviroments are due to settings in the respective .rb files.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文