试图在 Yaml 文件中隐藏一些 Twitter API 凭据,获取未初始化的常量 EnvironmentVariables::Application::YAML
Trying to hide some Twitter API credentials in a Yaml file, getting uninitialized constant EnvironmentVariables::Application::YAML
我有一个初始值设定项 environment_variables.rb
:
module EnvironmentVariables
class Application < Rails::Application
config.before_configuration do
env_file = Rails.root.join("config", 'environment_variables.yml').to_s
if File.exists?(env_file)
YAML.load_file(env_file)[Rails.env].each do |key, value|
ENV[key.to_s] = value
end # end YAML.load_file
end # end if File.exists?
end # end config.before_configuration
end # end class
end
我检查过的是在配置中找到一个名为 environment_variables.yml
的文件:
test:
TWITTER_CONSUMER_KEY: ""
TWITTER_CONSUMER_SECRET: ""
TWITTER_ACCESS_TOKEN: ""
TWITTER_ACCESS_TOKEN_SECRET: ""
development:
TWITTER_CONSUMER_KEY: ""
TWITTER_CONSUMER_SECRET: ""
TWITTER_ACCESS_TOKEN: ""
TWITTER_ACCESS_TOKEN_SECRET: ""
production:
TWITTER_CONSUMER_KEY: ""
TWITTER_CONSUMER_SECRET: ""
TWITTER_ACCESS_TOKEN: ""
TWITTER_ACCESS_TOKEN_SECRET: ""
但是我在尝试 运行 我的代码时遇到错误:
class TwitterAPI
def client
@client ||= Twitter.REST.Client.new do |config|
config.TWITTER_CONSUMER_KEY = ENV['TWITTER_CONSUMER_KEY']
config.TWITTER_CONSUMER_SECRET = ENV['TWITTER_CONSUMER_SECRET']
config.TWITTER_ACCESS_TOKEN = ENV['TWITTER_ACCESS_TOKEN']
config.TWITTER_ACCESS_TOKEN_SECRET = ENV['TWITTER_ACCESS_TOKEN_SECRET']
end
end
end
通过在规范中调用 TwitterAPI.new.client
来测试它。错误如下所示:
/Users/nathanielmots/Documents/Development/TakeStock/stock/config/initializers/environment_variables.rb:7:in `block in <class:Application>': uninitialized constant EnvironmentVariables::Application::YAML (NameError)
关于这个问题有什么建议吗?谢谢你的帮助。
uninitialized constant EnvironmentVariables::Application::YAML
表示,找不到YAML
的定义。因此,您需要在使用 YAML
.
的文件中要求它
在您的 environment_variables.rb
中,需要 yaml
:
require 'yaml'
这应该可以解决您的问题。
我有一个初始值设定项 environment_variables.rb
:
module EnvironmentVariables
class Application < Rails::Application
config.before_configuration do
env_file = Rails.root.join("config", 'environment_variables.yml').to_s
if File.exists?(env_file)
YAML.load_file(env_file)[Rails.env].each do |key, value|
ENV[key.to_s] = value
end # end YAML.load_file
end # end if File.exists?
end # end config.before_configuration
end # end class
end
我检查过的是在配置中找到一个名为 environment_variables.yml
的文件:
test:
TWITTER_CONSUMER_KEY: ""
TWITTER_CONSUMER_SECRET: ""
TWITTER_ACCESS_TOKEN: ""
TWITTER_ACCESS_TOKEN_SECRET: ""
development:
TWITTER_CONSUMER_KEY: ""
TWITTER_CONSUMER_SECRET: ""
TWITTER_ACCESS_TOKEN: ""
TWITTER_ACCESS_TOKEN_SECRET: ""
production:
TWITTER_CONSUMER_KEY: ""
TWITTER_CONSUMER_SECRET: ""
TWITTER_ACCESS_TOKEN: ""
TWITTER_ACCESS_TOKEN_SECRET: ""
但是我在尝试 运行 我的代码时遇到错误:
class TwitterAPI
def client
@client ||= Twitter.REST.Client.new do |config|
config.TWITTER_CONSUMER_KEY = ENV['TWITTER_CONSUMER_KEY']
config.TWITTER_CONSUMER_SECRET = ENV['TWITTER_CONSUMER_SECRET']
config.TWITTER_ACCESS_TOKEN = ENV['TWITTER_ACCESS_TOKEN']
config.TWITTER_ACCESS_TOKEN_SECRET = ENV['TWITTER_ACCESS_TOKEN_SECRET']
end
end
end
通过在规范中调用 TwitterAPI.new.client
来测试它。错误如下所示:
/Users/nathanielmots/Documents/Development/TakeStock/stock/config/initializers/environment_variables.rb:7:in `block in <class:Application>': uninitialized constant EnvironmentVariables::Application::YAML (NameError)
关于这个问题有什么建议吗?谢谢你的帮助。
uninitialized constant EnvironmentVariables::Application::YAML
表示,找不到YAML
的定义。因此,您需要在使用 YAML
.
在您的 environment_variables.rb
中,需要 yaml
:
require 'yaml'
这应该可以解决您的问题。