为什么我得到未定义的方法“each”?
Why I get undefined method `each'?
在 application.rb 中,我尝试读取 YAML 文件:
config.before_configuration do
env_file = File.join(Rails.root, 'config', 'local_env.yml')
YAML.load(File.open(env_file)).each do |key, value|
ENV[key.to_s] = value
end if File.exists?(env_file)
end
但是,我得到这个错误:
/var/www/config/application.rb:26:in `block in <class:Application>': undefined method `each' for #<String:0x00000007afb7f0> (NoMethodError)
from /usr/local/rvm/gems/ruby-2.2.1/gems/activesupport-4.1.6/lib/active_support/lazy_load_hooks.rb:36:in `call'
from /usr/local/rvm/gems/ruby-2.2.1/gems/activesupport-4.1.6/lib/active_support/lazy_load_hooks.rb:36:in `execute_hook'
from /usr/local/rvm/gems/ruby-2.2.1/gems/activesupport-4.1.6/lib/active_support/lazy_load_hooks.rb:28:in `block in on_load'
from /usr/local/rvm/gems/ruby-2.2.1/gems/activesupport-4.1.6/lib/active_support/lazy_load_hooks.rb:27:in `each'
from /usr/local/rvm/gems/ruby-2.2.1/gems/activesupport-4.1.6/lib/active_support/lazy_load_hooks.rb:27:in `on_load'
from /usr/local/rvm/gems/ruby-2.2.1/gems/railties-4.1.6/lib/rails/railtie/configuration.rb:53:in `before_configuration'
from /var/www/config/application.rb:24:in `<class:Application>'
有什么想法吗?
编辑
我的 yaml 文件:
LOAD_JS_FROM_AMAZON:no
RACK_ENV:production
S3_BUCKET_NAME:bucket_name
S3_CMS_BUCKET_NAME:cms_bucket_name
YAML.load(File.open(env_file))
您的 YAML 返回 String
而不是 Hash
冒号和值之间需要空格:
LOAD_JS_FROM_AMAZON: no
RACK_ENV: production
S3_BUCKET_NAME: bucket_name
S3_CMS_BUCKET_NAME: cms_bucket_name
在 application.rb 中,我尝试读取 YAML 文件:
config.before_configuration do
env_file = File.join(Rails.root, 'config', 'local_env.yml')
YAML.load(File.open(env_file)).each do |key, value|
ENV[key.to_s] = value
end if File.exists?(env_file)
end
但是,我得到这个错误:
/var/www/config/application.rb:26:in `block in <class:Application>': undefined method `each' for #<String:0x00000007afb7f0> (NoMethodError)
from /usr/local/rvm/gems/ruby-2.2.1/gems/activesupport-4.1.6/lib/active_support/lazy_load_hooks.rb:36:in `call'
from /usr/local/rvm/gems/ruby-2.2.1/gems/activesupport-4.1.6/lib/active_support/lazy_load_hooks.rb:36:in `execute_hook'
from /usr/local/rvm/gems/ruby-2.2.1/gems/activesupport-4.1.6/lib/active_support/lazy_load_hooks.rb:28:in `block in on_load'
from /usr/local/rvm/gems/ruby-2.2.1/gems/activesupport-4.1.6/lib/active_support/lazy_load_hooks.rb:27:in `each'
from /usr/local/rvm/gems/ruby-2.2.1/gems/activesupport-4.1.6/lib/active_support/lazy_load_hooks.rb:27:in `on_load'
from /usr/local/rvm/gems/ruby-2.2.1/gems/railties-4.1.6/lib/rails/railtie/configuration.rb:53:in `before_configuration'
from /var/www/config/application.rb:24:in `<class:Application>'
有什么想法吗?
编辑
我的 yaml 文件:
LOAD_JS_FROM_AMAZON:no
RACK_ENV:production
S3_BUCKET_NAME:bucket_name
S3_CMS_BUCKET_NAME:cms_bucket_name
YAML.load(File.open(env_file))
您的 YAML 返回 String
而不是 Hash
冒号和值之间需要空格:
LOAD_JS_FROM_AMAZON: no
RACK_ENV: production
S3_BUCKET_NAME: bucket_name
S3_CMS_BUCKET_NAME: cms_bucket_name