为什么我得到未定义的方法`每个'?

时间:2015-03-14 08:52:43

标签: ruby ruby-on-rails-4 yaml

在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

1 个答案:

答案 0 :(得分:2)

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