【问题标题】:rake undefined method `[]' for nil:NilClassrake undefined method `[]' for nil:NilClass
【发布时间】:2013-06-15 06:35:19
【问题描述】:

我刚刚在我的部署服务器上安装了一个 Rails 3.1 应用程序。

当我尝试运行时

sudo rake db:setup RAILS_ENV=“production”

我收到一条错误消息说

rake aborted!
undefined method `[]' for nil:NilClass

使用 --trace 它说:

** Invoke db:setup (first_time)  
** Invoke db:create (first_time)  
** Invoke db:load_config (first_time)  
** Invoke rails_env (first_time)  
** Execute rails_env  
** Execute db:load_config  
** Execute db:create  
rake aborted!  
undefined method '[]' for nil:NilClass  
/usr/local/lib/ruby/gems/1.9.1/gems/activerecord-3.1.0/lib/active_record/railties/databases.rake:74:in 'rescue in create_database'  
/usr/local/lib/ruby/gems/1.9.1/gems/activerecord-3.1.0/lib/active_record/railties/databases.rake:54:in 'create_database'  
/usr/local/lib/ruby/gems/1.9.1/gems/activerecord-3.1.0/lib/active_record/railties/databases.rake:44:in 'block (2 levels) in <top (required)>'  
/usr/local/lib/ruby/gems/1.9.1/gems/rake-0.9.2/lib/rake/task.rb:205:in 'call'  
/usr/local/lib/ruby/gems/1.9.1/gems/rake-0.9.2/lib/rake/task.rb:205:in 'block in execute'  
/usr/local/lib/ruby/gems/1.9.1/gems/rake-0.9.2/lib/rake/task.rb:200:in 'each'  
/usr/local/lib/ruby/gems/1.9.1/gems/rake-0.9.2/lib/rake/task.rb:200:in 'execute'  
/usr/local/lib/ruby/gems/1.9.1/gems/rake-0.9.2/lib/rake/task.rb:158:in 'block in invoke_with_call_chain'  
/usr/local/lib/ruby/1.9.1/monitor.rb:201:in 'mon_synchronize'  
/usr/local/lib/ruby/gems/1.9.1/gems/rake-0.9.2/lib/rake/task.rb:151:in 'invoke_with_call_chain'  
/usr/local/lib/ruby/gems/1.9.1/gems/rake-0.9.2/lib/rake/task.rb:176:in 'block in invoke_prerequisites'  
/usr/local/lib/ruby/gems/1.9.1/gems/rake-0.9.2/lib/rake/task.rb:174:in 'each'  
/usr/local/lib/ruby/gems/1.9.1/gems/rake-0.9.2/lib/rake/task.rb:174:in 'invoke_prerequisites'  
/usr/local/lib/ruby/gems/1.9.1/gems/rake-0.9.2/lib/rake/task.rb:157:in 'block in invoke_with_call_chain'  
/usr/local/lib/ruby/1.9.1/monitor.rb:201:in 'mon_synchronize'  
/usr/local/lib/ruby/gems/1.9.1/gems/rake-0.9.2/lib/rake/task.rb:151:in 'invoke_with_call_chain'  
/usr/local/lib/ruby/gems/1.9.1/gems/rake-0.9.2/lib/rake/task.rb:144:in 'invoke'  
/usr/local/lib/ruby/gems/1.9.1/gems/rake-0.9.2/lib/rake/application.rb:112:in 'invoke_task'    
/usr/local/lib/ruby/gems/1.9.1/gems/rake-0.9.2/lib/rake/application.rb:90:in 'block (2 levels) in top_level'  
/usr/local/lib/ruby/gems/1.9.1/gems/rake-0.9.2/lib/rake/application.rb:90:in 'each'  
/usr/local/lib/ruby/gems/1.9.1/gems/rake-0.9.2/lib/rake/application.rb:90:in 'block in top_level'  
/usr/local/lib/ruby/gems/1.9.1/gems/rake-0.9.2/lib/rake/application.rb:129:in 'standard_exception_handling'  
/usr/local/lib/ruby/gems/1.9.1/gems/rake-0.9.2/lib/rake/application.rb:84:in 'top_level'  
/usr/local/lib/ruby/gems/1.9.1/gems/rake-0.9.2/lib/rake/application.rb:62:in 'block in run'  
/usr/local/lib/ruby/gems/1.9.1/gems/rake-0.9.2/lib/rake/application.rb:129:in 'standard_exception_handling'  
/usr/local/lib/ruby/gems/1.9.1/gems/rake-0.9.2/lib/rake/application.rb:59:in 'run'  
/usr/local/lib/ruby/gems/1.9.1/gems/rake-0.9.2/bin/rake:32:in '<top (required)>'  
/usr/local/bin/rake:19:in 'load'  
/usr/local/bin/rake:19:in '<main>'  
Tasks: TOP => db:setup => db:create  

我的 database.yml 说

production:
adapter: mysql2  
    encoding: utf8  
    reconnect: false  
    database: t_production  
    pool: 5  
    username: deploy  
    password: V  
    host: localhost  

只有 1 次迁移,上面写着:

    class CreateDeals < ActiveRecord::Migration  
  def change  
    create_table :deals do |t|  
      t.string :title  
      t.text :description  
      t.string :image_url  
      t.decimal :price, :precision => 8, :scale => 2  
t.timestamps  
    end  
  end  
end  

我应该如何解决这个问题?我什至不知道从哪里开始。

【问题讨论】:

    标签: ruby-on-rails-3


    【解决方案1】:

    你应该修正你的陈述,双引号是错误的。它们需要是常规的" 引号。它可能会尝试为“production” 环境加载一个设置,这显然不存在。

    如果您使用正确的引号,请确保您的标识正确,定义应如下所示:

    production:
        adapter: mysql2  
        encoding: utf8  
        reconnect: false  
        database: t_production  
        pool: 5  
        username: deploy  
        password: V  
        host: localhost  
    

    【讨论】:

    • 刚刚尝试将引号更改为标准双引号。还尝试使用单引号和不使用引号,但仍然收到相同的错误消息。
    • 谢谢!缩进是问题所在。我没有意识到 yaml 有明显的缩进。我还更新了 rake。
    • YAML 是一种类似于haml 的标记语言。 html用标签做的地方,yaml和haml用缩进指定嵌套。
    • @Femaref 这太棒了!你应该把它放在你的答案中,这就是我遇到的确切问题!非常感谢!
    猜你喜欢
    • 2016-01-02
    • 2013-03-15
    • 1970-01-01
    • 1970-01-01
    • 2013-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多