Tuesday, January 31, 2012

Getting error 'undefined method `before_dispatch' for class `Rails::Rack::Logger' (NameError) '

Upgradation from Rails 3.1 to Rails 3.2 done successfully. When starting rails server getting following error:

/home/ritesh/.rvm/gems/ruby-1.9.3-p0/gems/activesupport-3.2.0/lib/active_support/core_ext/module/aliasing.rb:31:in `alias_method': undefined method `before_dispatch' for class `Rails::Rack::Logger' (NameError)
from /home/ritesh/.rvm/gems/ruby-1.9.3-p0/gems/activesupport-3.2.0/lib/active_support/core_ext/module/aliasing.rb:31:in `alias_method_chain'
from /home/ritesh/projects/projects/crowd/spec/dummy/config/initializers/quite_assets.rb:6:in `block in <top (required)>'
from /home/ritesh/projects/projects/crowd/spec/dummy/config/initializers/quite_assets.rb:2:in `class_eval'
from /home/ritesh/projects/projects/crowd/spec/dummy/config/initializers/quite_assets.rb:2:in `<top (required)>'
from /home/ritesh/.rvm/gems/ruby-1.9.3-p0/gems/activesupport-3.2.0/lib/active_support/dependencies.rb:245:in `load'
from /home/ritesh/.rvm/gems/ruby-1.9.3-p0/gems/activesupport-3.2.0/lib/active_support/dependencies.rb:245:in `block in load'
from /home/ritesh/.rvm/gems/ruby-1.9.3-p0/gems/activesupport-3.2.0/lib/active_support/dependencies.rb:236:in `load_dependency'
from /home/ritesh/.rvm/gems/ruby-1.9.3-p0/gems/activesupport-3.2.0/lib/active_support/dependencies.rb:245:in `load'
from /home/ritesh/.rvm/gems/ruby-1.9.3-p0/gems/railties-3.2.0/lib/rails/engine.rb:588:in `block (2 levels) in <class:Engine>'
from /home/ritesh/.rvm/gems/ruby-1.9.3-p0/gems/railties-3.2.0/lib/rails/engine.rb:587:in `each'
from /home/ritesh/.rvm/gems/ruby-1.9.3-p0/gems/railties-3.2.0/lib/rails/engine.rb:587:in `block in <class:Engine>'
from /home/ritesh/.rvm/gems/ruby-1.9.3-p0/gems/railties-3.2.0/lib/rails/initializable.rb:30:in `instance_exec'
from /home/ritesh/.rvm/gems/ruby-1.9.3-p0/gems/railties-3.2.0/lib/rails/initializable.rb:30:in `run'
from /home/ritesh/.rvm/gems/ruby-1.9.3-p0/gems/railties-3.2.0/lib/rails/initializable.rb:55:in `block in run_initializers'
from /home/ritesh/.rvm/gems/ruby-1.9.3-p0/gems/railties-3.2.0/lib/rails/initializable.rb:54:in `each'
from /home/ritesh/.rvm/gems/ruby-1.9.3-p0/gems/railties-3.2.0/lib/rails/initializable.rb:54:in `run_initializers'
from /home/ritesh/.rvm/gems/ruby-1.9.3-p0/gems/railties-3.2.0/lib/rails/application.rb:136:in `initialize!'
from /home/ritesh/.rvm/gems/ruby-1.9.3-p0/gems/railties-3.2.0/lib/rails/railtie/configurable.rb:30:in `method_missing'
from /home/ritesh/projects/projects/crowd/spec/dummy/config/environment.rb:5:in `<top (required)>'
from /home/ritesh/projects/projects/crowd/spec/dummy/config.ru:4:in `require'
from /home/ritesh/projects/projects/crowd/spec/dummy/config.ru:4:in `block in <main>'
from /home/ritesh/.rvm/gems/ruby-1.9.3-p0/gems/rack-1.4.1/lib/rack/builder.rb:51:in `instance_eval'
from /home/ritesh/.rvm/gems/ruby-1.9.3-p0/gems/rack-1.4.1/lib/rack/builder.rb:51:in `initialize'
from /home/ritesh/projects/projects/crowd/spec/dummy/config.ru:1:in `new'
from /home/ritesh/projects/projects/crowd/spec/dummy/config.ru:1:in `<main>'
from /home/ritesh/.rvm/gems/ruby-1.9.3-p0/gems/rack-1.4.1/lib/rack/builder.rb:40:in `eval'
from /home/ritesh/.rvm/gems/ruby-1.9.3-p0/gems/rack-1.4.1/lib/rack/builder.rb:40:in `parse_file'
from /home/ritesh/.rvm/gems/ruby-1.9.3-p0/gems/rack-1.4.1/lib/rack/server.rb:200:in `app'
from /home/ritesh/.rvm/gems/ruby-1.9.3-p0/gems/railties-3.2.0/lib/rails/commands/server.rb:46:in `app'
from /home/ritesh/.rvm/gems/ruby-1.9.3-p0/gems/rack-1.4.1/lib/rack/server.rb:301:in `wrapped_app'
from /home/ritesh/.rvm/gems/ruby-1.9.3-p0/gems/rack-1.4.1/lib/rack/server.rb:252:in `start'
from /home/ritesh/.rvm/gems/ruby-1.9.3-p0/gems/railties-3.2.0/lib/rails/commands/server.rb:70:in `start'
from /home/ritesh/.rvm/gems/ruby-1.9.3-p0/gems/railties-3.2.0/lib/rails/commands.rb:55:in `block in <top (required)>'
from /home/ritesh/.rvm/gems/ruby-1.9.3-p0/gems/railties-3.2.0/lib/rails/commands.rb:50:in `tap'
from /home/ritesh/.rvm/gems/ruby-1.9.3-p0/gems/railties-3.2.0/lib/rails/commands.rb:50:in `<top (required)>'
from /home/ritesh/projects/projects/crowd/spec/dummy/script/rails:6:in `require'
from /home/ritesh/projects/projects/crowd/spec/dummy/script/rails:6:in `<top (required)>'
from script/rails:6:in `load'
from script/rails:6:in `<main>'

To fix the error, 

Place the following code in config/initializers/quiet_assets.rb
Rails.application.assets.logger = Logger.new('/dev/null')
Rails::Rack::Logger.class_eval do
  def call_with_quiet_assets(env)
    previous_level = Rails.logger.level
    Rails.logger.level = Logger::ERROR if env['PATH_INFO'].index("/assets/") == 0
    call_without_quiet_assets(env).tap do
      Rails.logger.level = previous_level
    end
  end
  alias_method_chain :call, :quiet_assets
 end


3 comments:

  1. Thanks.

    The formatting is a bit wrong in the last line. The 'end' at the end of line should be on next line by itself :-)

    ReplyDelete
  2. Thanks HakonB, I have corrected that error, Ritesh Kumar

    ReplyDelete
  3. Thanks to your tip I have fixed my app much quicker than written this comment;) Cheers.

    ReplyDelete