gpt4 book ai didi

ruby-on-rails - 如何在 Controller 和模型之间共享常量 Ruby on Rails

转载 作者:行者123 更新时间:2023-12-04 07:38:42 25 4
gpt4 key购买 nike

是否可以在 Controller 和模型之间共享常量?

例如在 product.rb我有

PRODUCT_TYPES = %w[one two]

我要 PRODUCT_TYPES常量在 Controller 中也可用。

最佳答案

根据我的考虑,您的 产品 模型看起来像

 class Product < ActiveRecord::Base
PRODUCT_TYPES = %w[one two]
end

您可以访问 Controller 中的给定常量,如下所示
p.product_type == Product::PRODUCT_TYPES[:one]

关于ruby-on-rails - 如何在 Controller 和模型之间共享常量 Ruby on Rails,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20920441/

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