NoMethodError:未定义的方法`[]' for nil:资产的NilClass:在heroku上预编译

时间:2015-02-18 10:00:36

标签: ruby-on-rails ruby ruby-on-rails-4 heroku devise

使用devise和omniauth的应用程序在localhost上运行良好,但是当我在heroku上部署它时,我收到错误:

-----> Preparing app for Rails asset pipeline
       Running: rake assets:precompile
       rake aborted!
       NoMethodError: undefined method `[]' for nil:NilClass
       /tmp/build_e711adadeab2d8cb9d37632e4c0ba08b/config/initializers/omniauth.rb:6:in `block in <top (required)>'
       /tmp/build_e711adadeab2d8cb9d37632e4c0ba08b/vendor/bundle/ruby/2.1.0/gems/rack-1.5.2/lib/rack/builder.rb:55:in `instance_eval'
       /tmp/build_e711adadeab2d8cb9d37632e4c0ba08b/vendor/bundle/ruby/2.1.0/gems/rack-1.5.2/lib/rack/builder.rb:55:in `initialize'
       /tmp/build_e711adadeab2d8cb9d37632e4c0ba08b/vendor/bundle/ruby/2.1.0/gems/omniauth-1.2.2/lib/omniauth/builder.rb:6:in `initialize'
       /tmp/build_e711adadeab2d8cb9d37632e4c0ba08b/vendor/bundle/ruby/2.1.0/gems/actionpack-4.1.4/lib/action_dispatch/middleware/stack.rb:43:in `new'
       /tmp/build_e711adadeab2d8cb9d37632e4c0ba08b/vendor/bundle/ruby/2.1.0/gems/actionpack-4.1.4/lib/action_dispatch/middleware/stack.rb:43:in `build'
       /tmp/build_e711adadeab2d8cb9d37632e4c0ba08b/vendor/bundle/ruby/2.1.0/gems/actionpack-4.1.4/lib/action_dispatch/middleware/stack.rb:118:in `block in build'
       /tmp/build_e711adadeab2d8cb9d37632e4c0ba08b/vendor/bundle/ruby/2.1.0/gems/actionpack-4.1.4/lib/action_dispatch/middleware/stack.rb:118:in `each'
       /tmp/build_e711adadeab2d8cb9d37632e4c0ba08b/vendor/bundle/ruby/2.1.0/gems/actionpack-4.1.4/lib/action_dispatch/middleware/stack.rb:118:in `inject'
       /tmp/build_e711adadeab2d8cb9d37632e4c0ba08b/vendor/bundle/ruby/2.1.0/gems/actionpack-4.1.4/lib/action_dispatch/middleware/stack.rb:118:in `build'
       /tmp/build_e711adadeab2d8cb9d37632e4c0ba08b/vendor/bundle/ruby/2.1.0/gems/railties-4.1.4/lib/rails/engine.rb:498:in `app'
       /tmp/build_e711adadeab2d8cb9d37632e4c0ba08b/vendor/bundle/ruby/2.1.0/gems/railties-4.1.4/lib/rails/application/finisher.rb:36:in `block in <module:Finisher>'
       /tmp/build_e711adadeab2d8cb9d37632e4c0ba08b/vendor/bundle/ruby/2.1.0/gems/railties-4.1.4/lib/rails/initializable.rb:30:in `instance_exec'
       /tmp/build_e711adadeab2d8cb9d37632e4c0ba08b/vendor/bundle/ruby/2.1.0/gems/railties-4.1.4/lib/rails/initializable.rb:30:in `run'
       /tmp/build_e711adadeab2d8cb9d37632e4c0ba08b/vendor/bundle/ruby/2.1.0/gems/railties-4.1.4/lib/rails/initializable.rb:55:in `block in run_initializers'
       /tmp/build_e711adadeab2d8cb9d37632e4c0ba08b/vendor/bundle/ruby/2.1.0/gems/railties-4.1.4/lib/rails/initializable.rb:54:in `run_initializers'
       /tmp/build_e711adadeab2d8cb9d37632e4c0ba08b/vendor/bundle/ruby/2.1.0/gems/railties-4.1.4/lib/rails/application.rb:300:in `initialize!'
       /tmp/build_e711adadeab2d8cb9d37632e4c0ba08b/config/environment.rb:5:in `<top (required)>'
       /tmp/build_e711adadeab2d8cb9d37632e4c0ba08b/vendor/bundle/ruby/2.1.0/gems/activesupport-4.1.4/lib/active_support/dependencies.rb:247:in `require'
       /tmp/build_e711adadeab2d8cb9d37632e4c0ba08b/vendor/bundle/ruby/2.1.0/gems/activesupport-4.1.4/lib/active_support/dependencies.rb:247:in `block in require'
       /tmp/build_e711adadeab2d8cb9d37632e4c0ba08b/vendor/bundle/ruby/2.1.0/gems/activesupport-4.1.4/lib/active_support/dependencies.rb:232:in `load_dependency'
       /tmp/build_e711adadeab2d8cb9d37632e4c0ba08b/vendor/bundle/ruby/2.1.0/gems/activesupport-4.1.4/lib/active_support/dependencies.rb:247:in `require'
       /tmp/build_e711adadeab2d8cb9d37632e4c0ba08b/vendor/bundle/ruby/2.1.0/gems/railties-4.1.4/lib/rails/application.rb:276:in `require_environment!'
       /tmp/build_e711adadeab2d8cb9d37632e4c0ba08b/vendor/bundle/ruby/2.1.0/gems/railties-4.1.4/lib/rails/application.rb:379:in `block in run_tasks_blocks'
       /tmp/build_e711adadeab2d8cb9d37632e4c0ba08b/vendor/bundle/ruby/2.1.0/gems/sprockets-rails-2.2.2/lib/sprockets/rails/task.rb:64:in `block (2 levels) in define'
       Tasks: TOP => environment
       (See full trace by running task with --trace)
 !
 !     Precompiling assets failed.
 !

 !     Push rejected, failed to compile Ruby app

To git@heroku.com:my-app.git
 ! [remote rejected] master -> master (pre-receive hook declined)

这是Gemfile和omniauth文件:

#Gemfile:
#....
gem 'google-api-client'
gem 'omniauth'
gem 'omniauth-google-oauth2'
gem 'devise'
#.....


#config/initializers/omniauth.rb

# probably this isn't necessary
if Rails.env.production?
  OmniAuth.config.full_host = 'https://my-app.herokuapp.com'
end

# but it is necessary
Rails.application.config.middleware.use OmniAuth::Builder do
  provider(:google_oauth2, $ga_client['client_id'], $ga_client['client_secret'], 
    { scope: 'userinfo.profile, userinfo.email, analytics', access_type: 'offline', prompt: 'consent' }
  )
end

3 个答案:

答案 0 :(得分:2)

在您的代码中,$ ga_client为零。您没有在文件中的任何位置定义这样的数组,如果在其他地方定义,则在执行此初始化程序时不会加载它。

尝试按照文档中的建议设置client_id和client_secret以及环境变量。

provider :google_auth2, ENV['GOOGLE_KEY'], ENV['GOOGLE_SECRET']

答案 1 :(得分:0)

全局变量未初始化:

# but it is necessary

Rails.application.config.middleware.use OmniAuth::Builder do
  provider(:google_oauth2, $ga_client['client_id'], $ga_client['client_secret'], 
    { scope: 'userinfo.profile, userinfo.email, analytics', access_type: 'offline', prompt: 'consent' }
  )
end

# ops $ga_client is nil

我用以下代码替换上面的代码时发现了它:

Rails.application.config.middleware.use OmniAuth::Builder do
  provider :google_oauth2, ENV["GOOGLE_CLIENT_ID"], ENV["GOOGLE_CLIENT_SECRET"]
end

答案 2 :(得分:-1)

我遇到了同样的问题。导致此问题的原因是Rails使用名为 sqlite3 的数据库系统,Heroku不使用此数据库。要解决此问题,请转到 Gemfile 并向下滚动,直到看到看起来有点像这样的内容

group :development, :test do
  gem 'byebug'
  gem 'web-console', '~> 2.0'
end

注意:如果您使用的是低于Rails 4.0.0的任何版本,您将不会拥有此组,只需创建它即可。

现在位于Gemfile顶部的某个位置会显示

gem 'sqlite3'

将此行移至开发,测试组现在这只会在开发中运行。您现在可以通过编写

再次尝试创建Heroku应用程序
$ heroku create

然后您可能需要迁移数据库。通过运行

来完成此操作
$ rake db:migrate

现在推送到Heroku

$ heroku push

这应该有用!

希望我帮忙;)