【发布时间】:2015-11-24 22:15:23
【问题描述】:
我有一个初始化器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)
对这个问题有什么建议吗?谢谢你的帮助。
【问题讨论】:
-
你
require 'yaml'在哪里吗?
标签: ruby-on-rails environment-variables yaml