gpt4 book ai didi

ruby-on-rails - Rails 应用程序 : autoloading classes defined in modules

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

在 Rails 5.0.1 应用程序中,我的文件 app/actions/frontend/cart/get_cart_items_summarized.rb 包含:

module Actions
module Frontend
module Cart
class GetCartItemsSummarized
#content here
end
end
end
end

app/helpers/application_helper.rb 中,我称之为:

def get_cart_items
#...
items = Actions::Frontend::Cart::GetCartItemsSummarized.new.call
#...
end

但是我得到:

未初始化常量 ApplicationHelper::Actions

为什么?我应该如何使用这个类?

谢谢

最佳答案

在 rails/autoloading 中,第一级目录,即 app 下的目录,不被视为名称的一部分。这样您的模型就可以只是 User 而不是 Models::User

我的解决方案是将所有自定义内容放在 app/lib 中。这样,lib 就吃掉了那个非命名层,而你的文件夹结构的其余部分变成了一个名字。在你的例子中,把你的文件放到

app/lib/actions/frontend/cart/get_cart_items_summarized.rb

当然,您可以随意将“lib”替换为您想要的任何内容(例如“app/custom”)。这个名字不重要。

关于ruby-on-rails - Rails 应用程序 : autoloading classes defined in modules,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43998837/

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