在Rails中存储(结构化)配置数据的位置

时间:2022-04-15 23:06:59

For the Rails 3 application I'm writing, I am considering reading some of the configuration data from XML, YAML or JSON files on the local filesystem.

对于我正在编写的Rails 3应用程序,我正在考虑从本地文件系统上的XML,YAML或JSON文件中读取一些配置数据。

The point is: where should I put those files? Is there any default location in Rails apps where to store this kind of content?

关键是:我应该把这些文件放在哪里? Rails应用程序中是否存在存储此类内容的默认位置?

As a side note, my app is deployed on Heroku.

作为旁注,我的应用程序部署在Heroku上。

4 个解决方案

#1


8  

What I always do is:

我一直在做的是:

  • If the file is a general configuration file: I create a YAML file in the directory /config with one upper class key per environment
  • 如果文件是常规配置文件:我在目录/ config中创建一个YAML文件,每个环境有一个上层类密钥
  • If I have a file for each environment (big project): I create one YAML per environment and store them in /config/environments/
  • 如果我有一个文件用于每个环境(大项目):我为每个环境创建一个YAML并将它们存储在/ config / environments /

Then I create an initializer where I load the YAML, I symbolize the keys of the config hash and assign it to a constant like APP_CONFIG

然后我创建一个初始化器,我加载YAML,我象征着配置哈希的键,并将它分配给像APP_CONFIG这样的常量

#2


4  

I will ususaly adopt this method :

我将通常采用这种方法:

a config/config.yml

一个config / config.yml

development:
  another_key: "test"
  app_name: "My App"
test:
  another_key: "test"
production:
  prova: "ciao"

then create a ostruct in a initializer

然后在初始化程序中创建一个ostruct

#config/initializer/load_config.rb
require 'ostruct'
config = OpenStruct.new(YAML.load_file("#{RAILS_ROOT}/config/config.yml"))
::AppSetting = OpenStruct.new(config.send(RAILS_ENV))

No DB table, per environment setup and you could retrive info in a simple way

没有数据库表,每个环境设置,您可以以简单的方式检索信息

AppSetting.another_key
AppSetting.app_name

here a reference
have a nice day!

在这里参考有一个美好的一天!

#3


1  

You can also include it in a model so you can call Settings.var_name from anywhere in your app and it will parse the file for the right environment.

您还可以将其包含在模型中,以便可以从应用程序的任何位置调用Settings.var_name,它将解析文件以获得正确的环境。

With settingslogic gem:

使用settingslogic gem:

class Settings < Settingslogic
  source "#{Rails.root}/config/settings.yml"
  namespace Rails.env
end

#4


1  

Rails creates a config directory by default, containing a lot of configuration info for your application, including the database and environment information. I think that's a logical first place to consider.

Rails默认创建一个config目录,包含应用程序的大量配置信息,包括数据库和环境信息。我认为这是一个合乎逻辑的第一个考虑因素。

A second choice would be the app directory, which contains all the models, views and controllers for the application, but I think of that directory as containing executable code and its templates, so I'd go with the config directory, personally.

第二个选择是应用程序目录,其中包含应用程序的所有模型,视图和控制器,但我认为该目录包含可执行代码及其模板,因此我个人使用config目录。

#1


8  

What I always do is:

我一直在做的是:

  • If the file is a general configuration file: I create a YAML file in the directory /config with one upper class key per environment
  • 如果文件是常规配置文件:我在目录/ config中创建一个YAML文件,每个环境有一个上层类密钥
  • If I have a file for each environment (big project): I create one YAML per environment and store them in /config/environments/
  • 如果我有一个文件用于每个环境(大项目):我为每个环境创建一个YAML并将它们存储在/ config / environments /

Then I create an initializer where I load the YAML, I symbolize the keys of the config hash and assign it to a constant like APP_CONFIG

然后我创建一个初始化器,我加载YAML,我象征着配置哈希的键,并将它分配给像APP_CONFIG这样的常量

#2


4  

I will ususaly adopt this method :

我将通常采用这种方法:

a config/config.yml

一个config / config.yml

development:
  another_key: "test"
  app_name: "My App"
test:
  another_key: "test"
production:
  prova: "ciao"

then create a ostruct in a initializer

然后在初始化程序中创建一个ostruct

#config/initializer/load_config.rb
require 'ostruct'
config = OpenStruct.new(YAML.load_file("#{RAILS_ROOT}/config/config.yml"))
::AppSetting = OpenStruct.new(config.send(RAILS_ENV))

No DB table, per environment setup and you could retrive info in a simple way

没有数据库表,每个环境设置,您可以以简单的方式检索信息

AppSetting.another_key
AppSetting.app_name

here a reference
have a nice day!

在这里参考有一个美好的一天!

#3


1  

You can also include it in a model so you can call Settings.var_name from anywhere in your app and it will parse the file for the right environment.

您还可以将其包含在模型中,以便可以从应用程序的任何位置调用Settings.var_name,它将解析文件以获得正确的环境。

With settingslogic gem:

使用settingslogic gem:

class Settings < Settingslogic
  source "#{Rails.root}/config/settings.yml"
  namespace Rails.env
end

#4


1  

Rails creates a config directory by default, containing a lot of configuration info for your application, including the database and environment information. I think that's a logical first place to consider.

Rails默认创建一个config目录,包含应用程序的大量配置信息,包括数据库和环境信息。我认为这是一个合乎逻辑的第一个考虑因素。

A second choice would be the app directory, which contains all the models, views and controllers for the application, but I think of that directory as containing executable code and its templates, so I'd go with the config directory, personally.

第二个选择是应用程序目录,其中包含应用程序的所有模型,视图和控制器,但我认为该目录包含可执行代码及其模板,因此我个人使用config目录。