如何在rails应用程序中包含yml文件?

时间:2011-07-29 00:06:20

标签: ruby-on-rails yaml

我创建了一个名为oauth.yml的yml文件。我希望它包含在rails应用程序中。我应该把它包括在内。

1 个答案:

答案 0 :(得分:6)

您应该将它包含在配置文件夹下:

app/config/oauth.yml

让我们假设您的文件是这样的:

development:
  key: some_key
  secret: some_secret
test:
  key: some_key
  secret: some_secret

现在,在rails应用程序中的某个位置,可能在初始化程序文件中(在app / config / initializers下),您将此文件加载到变量或类:

all_oauth_config = YAML.load_file( File.join( Rails.root, 'app', 'config', 'oauth.yml' ) )
current_oauth_config = all_oauth_config[Rails.env] 
#now do something with this current_oauth_config variable
相关问题