Rails:如何使用rails app_config变量作为功能标志,在模型和控制器中设置条件功能

我需要在rails应用程序的app_config.yml中设置一个特性标志"feature_enabled“。在生产环境中,该标志为false,在登台环境中为true。在我的模型和控制器中启用一个特性,访问配置文件中的feature_enabled标志并将其用作条件(见下文),这是最好的方法吗?

app_config.yml

staging:  &staging
<<: *defaults
feature_enabled: true

production:
<<: *staging
feature_enabled: false

model_controller.rb:

def create
   if Rails.application.config.feature_enabled 
      #feature code
   end
end

model.rb:

class modelName < ApplicationRecord
   if Rails.application.config.feature_enabled 
        after_save :feature_method
        validates_presence_of :feature_attribute
   end
end

转载请注明出处:http://www.hjsszs.com/article/20230526/2163711.html