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

Kum*_*mar 2 yaml ruby-on-rails

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

Mau*_*res 6

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

app/config/oauth.yml
Run Code Online (Sandbox Code Playgroud)

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

development:
  key: some_key
  secret: some_secret
test:
  key: some_key
  secret: some_secret
Run Code Online (Sandbox Code Playgroud)

现在,在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
Run Code Online (Sandbox Code Playgroud)