4
3

More than 5 years have passed since last update.

Rails 側のエラー?

Posted at

(このメモは自分の個人ブログに掲載していたものを Qiita の方がふさわしいと考えて移したもの、2013年11月頃の情報です)

Rails 4 で開発してきて、development 環境では問題ないアプリを production 環境指定で動かしたらこんなエラーが出た。

NoMethodError (undefined method `before' for nil:NilClass):
  vendor/bundle/ruby/2.0.0/gems/activesupport-4.0.1/lib/active_support/callbacks.rb:284:in `_callback_before_5'
  vendor/bundle/ruby/2.0.0/gems/activesupport-4.0.1/lib/active_support/callbacks.rb:418:in `_run__1259989443451810935__process_action__callbacks'
  vendor/bundle/ruby/2.0.0/gems/activesupport-4.0.1/lib/active_support/callbacks.rb:80:in `run_callbacks'
  vendor/bundle/ruby/2.0.0/gems/actionpack-4.0.1/lib/abstract_controller/callbacks.rb:17:in `process_action'
  vendor/bundle/ruby/2.0.0/gems/actionpack-4.0.1/lib/action_controller/metal/rescue.rb:29:in `process_action'
  vendor/bundle/ruby/2.0.0/gems/actionpack-4.0.1/lib/action_controller/metal/instrumentation.rb:31:in `block in process_action'
  vendor/bundle/ruby/2.0.0/gems/activesupport-4.0.1/lib/active_support/notifications.rb:159:in `block in instrument'
  vendor/bundle/ruby/2.0.0/gems/activesupport-4.0.1/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
  vendor/bundle/ruby/2.0.0/gems/activesupport-4.0.1/lib/active_support/notifications.rb:159:in `instrument'
  vendor/bundle/ruby/2.0.0/gems/actionpack-4.0.1/lib/action_controller/metal/instrumentation.rb:30:in `process_action'
  vendor/bundle/ruby/2.0.0/gems/actionpack-4.0.1/lib/action_controller/metal/params_wrapper.rb:245:in `process_action'
  vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.1/lib/active_record/railties/controller_runtime.rb:18:in `process_action'
  vendor/bundle/ruby/2.0.0/gems/actionpack-4.0.1/lib/abstract_controller/base.rb:136:in `process'
  vendor/bundle/ruby/2.0.0/gems/actionpack-4.0.1/lib/abstract_controller/rendering.rb:44:in `process'
  vendor/bundle/ruby/2.0.0/gems/actionpack-4.0.1/lib/action_controller/metal.rb:195:in `dispatch'
  vendor/bundle/ruby/2.0.0/gems/actionpack-4.0.1/lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
  vendor/bundle/ruby/2.0.0/gems/actionpack-4.0.1/lib/action_controller/metal.rb:231:in `block in action'
  vendor/bundle/ruby/2.0.0/gems/actionpack-4.0.1/lib/action_dispatch/routing/route_set.rb:80:in `call'
  vendor/bundle/ruby/2.0.0/gems/actionpack-4.0.1/lib/action_dispatch/routing/route_set.rb:80:in `dispatch'
  vendor/bundle/ruby/2.0.0/gems/actionpack-4.0.1/lib/action_dispatch/routing/route_set.rb:48:in `call'
  vendor/bundle/ruby/2.0.0/gems/actionpack-4.0.1/lib/action_dispatch/journey/router.rb:71:in `block in call'
  vendor/bundle/ruby/2.0.0/gems/actionpack-4.0.1/lib/action_dispatch/journey/router.rb:59:in `each'
  vendor/bundle/ruby/2.0.0/gems/actionpack-4.0.1/lib/action_dispatch/journey/router.rb:59:in `call'
  vendor/bundle/ruby/2.0.0/gems/actionpack-4.0.1/lib/action_dispatch/routing/route_set.rb:680:in `call'
  vendor/bundle/ruby/2.0.0/gems/omniauth-1.1.4/lib/omniauth/strategy.rb:184:in `call!'
  vendor/bundle/ruby/2.0.0/gems/omniauth-1.1.4/lib/omniauth/strategy.rb:164:in `call'
  vendor/bundle/ruby/2.0.0/gems/omniauth-1.1.4/lib/omniauth/strategy.rb:184:in `call!'
  vendor/bundle/ruby/2.0.0/gems/omniauth-1.1.4/lib/omniauth/strategy.rb:164:in `call'
  vendor/bundle/ruby/2.0.0/gems/omniauth-1.1.4/lib/omniauth/builder.rb:49:in `call'
  vendor/bundle/ruby/2.0.0/gems/rack-1.5.2/lib/rack/etag.rb:23:in `call'
  vendor/bundle/ruby/2.0.0/gems/rack-1.5.2/lib/rack/conditionalget.rb:25:in `call'
  vendor/bundle/ruby/2.0.0/gems/rack-1.5.2/lib/rack/head.rb:11:in `call'
  vendor/bundle/ruby/2.0.0/gems/actionpack-4.0.1/lib/action_dispatch/middleware/params_parser.rb:27:in `call'
  vendor/bundle/ruby/2.0.0/gems/actionpack-4.0.1/lib/action_dispatch/middleware/flash.rb:241:in `call'
  vendor/bundle/ruby/2.0.0/gems/rack-1.5.2/lib/rack/session/abstract/id.rb:225:in `context'
  vendor/bundle/ruby/2.0.0/gems/rack-1.5.2/lib/rack/session/abstract/id.rb:220:in `call'
  vendor/bundle/ruby/2.0.0/gems/actionpack-4.0.1/lib/action_dispatch/middleware/cookies.rb:486:in `call'
  vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.1/lib/active_record/query_cache.rb:36:in `call'
  vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:626:in `call'
  vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.1/lib/active_record/migration.rb:369:in `call'
  vendor/bundle/ruby/2.0.0/gems/actionpack-4.0.1/lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
  vendor/bundle/ruby/2.0.0/gems/activesupport-4.0.1/lib/active_support/callbacks.rb:374:in `_run__4402304158781031014__call__callbacks'
  vendor/bundle/ruby/2.0.0/gems/activesupport-4.0.1/lib/active_support/callbacks.rb:80:in `run_callbacks'
  vendor/bundle/ruby/2.0.0/gems/actionpack-4.0.1/lib/action_dispatch/middleware/callbacks.rb:27:in `call'
  vendor/bundle/ruby/2.0.0/gems/actionpack-4.0.1/lib/action_dispatch/middleware/reloader.rb:64:in `call'
  vendor/bundle/ruby/2.0.0/gems/actionpack-4.0.1/lib/action_dispatch/middleware/remote_ip.rb:76:in `call'
  vendor/bundle/ruby/2.0.0/gems/actionpack-4.0.1/lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
  vendor/bundle/ruby/2.0.0/gems/actionpack-4.0.1/lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
  vendor/bundle/ruby/2.0.0/gems/railties-4.0.1/lib/rails/rack/logger.rb:38:in `call_app'
  vendor/bundle/ruby/2.0.0/gems/railties-4.0.1/lib/rails/rack/logger.rb:20:in `block in call'
  vendor/bundle/ruby/2.0.0/gems/activesupport-4.0.1/lib/active_support/tagged_logging.rb:67:in `block in tagged'
  vendor/bundle/ruby/2.0.0/gems/activesupport-4.0.1/lib/active_support/tagged_logging.rb:25:in `tagged'
  vendor/bundle/ruby/2.0.0/gems/activesupport-4.0.1/lib/active_support/tagged_logging.rb:67:in `tagged'
  vendor/bundle/ruby/2.0.0/gems/railties-4.0.1/lib/rails/rack/logger.rb:20:in `call'
  vendor/bundle/ruby/2.0.0/gems/actionpack-4.0.1/lib/action_dispatch/middleware/request_id.rb:21:in `call'
  vendor/bundle/ruby/2.0.0/gems/rack-1.5.2/lib/rack/methodoverride.rb:21:in `call'
  vendor/bundle/ruby/2.0.0/gems/rack-1.5.2/lib/rack/runtime.rb:17:in `call'
  vendor/bundle/ruby/2.0.0/gems/activesupport-4.0.1/lib/active_support/cache/strategy/local_cache.rb:83:in `call'
  vendor/bundle/ruby/2.0.0/gems/rack-1.5.2/lib/rack/lock.rb:17:in `call'
  vendor/bundle/ruby/2.0.0/gems/actionpack-4.0.1/lib/action_dispatch/middleware/static.rb:64:in `call'
  vendor/bundle/ruby/2.0.0/gems/rack-1.5.2/lib/rack/sendfile.rb:112:in `call'
  vendor/bundle/ruby/2.0.0/gems/railties-4.0.1/lib/rails/engine.rb:511:in `call'
  vendor/bundle/ruby/2.0.0/gems/railties-4.0.1/lib/rails/application.rb:97:in `call'
  vendor/bundle/ruby/2.0.0/gems/rack-1.5.2/lib/rack/lock.rb:17:in `call'
  vendor/bundle/ruby/2.0.0/gems/rack-1.5.2/lib/rack/content_length.rb:14:in `call'
  vendor/bundle/ruby/2.0.0/gems/rack-1.5.2/lib/rack/handler/webrick.rb:60:in `service'
  /Users/noto/.rbenv/versions/2.0.0-p247/lib/ruby/2.0.0/webrick/httpserver.rb:138:in `service'
  /Users/noto/.rbenv/versions/2.0.0-p247/lib/ruby/2.0.0/webrick/httpserver.rb:94:in `run'
  /Users/noto/.rbenv/versions/2.0.0-p247/lib/ruby/2.0.0/webrick/server.rb:295:in `block in start_thread'

結論から言うと、アプリ側の application_controller.rb 内で

before_action :foo,  (:bar unless Rails.env.production?)

みたいな書き方をしていて、production 環境だと括弧内が nil になり、それが一番上のところまで伝わって nil:NilClass になっているという問題だった。

しかし、あまり使い慣れていないフレームワークでフレームワーク側のエラーしか出ないと途方にくれる。検索しても何も情報出てこないし、Rails 4.0.0 から 4.0.1 に upgrade しても変わらないし。

で、じっさいのところ何をやったかというと、Pry を使っているので上記のスタックトレースの一番上の行に示される位置あたりに binding.pry を入れてブレークポイントにし、まわりの値を見ながら呼び出し元を探っていくという感じ。呼び出し元は組み込みメソッドである caller の結果を見ればいいということなので、これを見ながら進めていったのだが、active_support/callbacks.rb → abstract_controller/callbacks.rb とたどっていったらその次の呼び出し元は自分が書いた application_controller.rb だと気づいた。

つまり実行時にエラーとして表示されるスタックトレースと、Pry を使って caller を叩いてみるのとでは表示される内容が異なることがある、ということらしい。なので、はやく caller を叩いてみれば、上記のような「ぜんぶフレームワークのエラーじゃん」という時でも、自分のアプリのコードにたどり着けることがあるみたい。

原因がまったくわからなくて途方に暮れるという感覚が個人的に久しぶりで恐ろしかったんだけど、地道に調べていけば原因にたどり着けるという感覚も久しぶりで、ちょっとほっとしたり。

ところで、やっぱりリストの中に nil が含まれているくらいでフレームワーク側のエラーしか表示されないのって不親切だと思うので、abstract_controller/callbacks.rb を

      def _insert_callbacks(callbacks, block = nil)
        options = callbacks.last.is_a?(Hash) ? callbacks.pop : {}
        _normalize_callback_options(options)
        callbacks.push(block) if block
        callbacks.each do |callback|
+         next if callback == nil
          yield callback, options
        end
      end

って感じにすればいいのにって思うんだけど、どうなんでしょうね?

アプリ側で

before_action :foo
before_action :bar unless Rails.env.production?

って複数行で書けば回避できるのですが。

4
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
3