简体   繁体   English

从Controller更新Rails配置文件

[英]Update rails config file from Controller

I have a app running on rails, that loads config from a yaml file. 我有一个运行在Rails上的应用程序,可从yaml文件加载配置。 I'm trying to figure out the best way to update this from a controller, or should I be using a ActiveModel for my config? 我正在尝试找出从控制器更新此内容的最佳方法,还是应该在配置中使用ActiveModel?

The configuration is based on the answer here: Accessing config from application.rb in Controller (Rails 3) 该配置基于此处的答案: 从Controller中的application.rb访问配置(Rails 3)

config/my_app_config.yaml contains the configuration. config / my_app_config.yaml包含配置。

An initializer in config/initializerz/app_config.rb loads this: config / initializerz / app_config.rb中的初始化程序将加载以下内容:

AppConfig.config = YAML.load_file("config/my_app_config.yaml")[Rails.env].symbolize_keys AppConfig.config = YAML.load_file(“ config / my_app_config.yaml”)[Rails.env] .symbolize_keys

I can easily access this config from a controller and display it in a view by accessing MyAppConfig.config[:key_name], and update it from user input, but is there a recommended way to write the config hash back to the config file? 我可以轻松地从控制器访问此配置,并通过访问MyAppConfig.config [:key_name]在视图中显示它,并从用户输入中对其进行更新,但是是否存在建议的方法将配置哈希值写回到配置文件?

IMHO it's should be avoided. 恕我直言,应该避免。 If nature of your config is dynamic (changes in time) then you should just read it from DB as you read User data or whatever. 如果配置的性质是动态的(时间变化),则应在读取用户数据或其他任何内容时从数据库读取它。 If it's static then you may keep it in hash (YAML file, when config changes you just deploy your app). 如果它是静态的,则可以将其保留在哈希中(YAML文件,当配置更改时,只需部署应用程序即可)。 If settings change frequently I would recommend db. 如果设置经常更改,我建议使用db。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM