gpt4 book ai didi

ruby-on-rails - 为什么在 Rails 中使用/app/lib 而不是/lib?

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

在 sidekiq 文档中,有 this quote关于在与自动加载错误相关的 Rails 项目中更喜欢使用 /app/lib 而不是 /lib:

A lib/ directory will only cause pain. Move the code to app/lib/ and make sure the code inside follows the class/filename conventions.

此外,还有:

Don't configure extra paths in autoload_paths or eager_load_paths. That's a hack; follow the conventions! Any directory underneath app/ may contain Ruby code, you don't need to explicitly configure anything.

我的问题是:

这些使用 /app/lib/lib 更好的说法是否属实?

这是否只对自动加载 Rails 相关对象(例如 AR 模型、 Controller 、作业等)有用?或者它也会帮助 PORO 吗?

这些评论是否只有在特定的上下文中才有意义?

最佳答案

根据我的经验,app/lib 更易于使用。您可以从字面上坚持使用 Class MathFunction 之类的东西,并通过 MathFunction.sqrRoot 在其他地方(例如 Controller 或模块)使用它。

要使用/lib,您需要使用autoload_paths 配置您的Rails 应用程序。 autoload_paths 也需要一些调整才能在 production 中正常工作. Matz 本人不鼓励 autoload 因为它在 being deprecated 的进程中.

我唯一一次需要使用 lib 目录是为了制作自定义 rake 任务。否则我坚持使用 app/lib

关于ruby-on-rails - 为什么在 Rails 中使用/app/lib 而不是/lib?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52284058/

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