views:

1590

answers:

5

I installed some new gems for testing and ran into an uninitialized constant ActionController::Dispatcher::MiddlewareStack error. I followed the instructions on the gem rdocs-specified the gem dependency in my environment.rb file and then ran rake gems:install and rake gems:unpack.

I also copied over my environment.rb file one line at a time into a different project and I did not get the error. I was wondering what other things could be going wrong.

Here is a longer version of the error (I first ran into the error running a simple test; same thing happens in a console when I do require 'active_support'):

$ ruby test/unit/user_test.rb 
** has_many_polymorphs: rails environment detected
/Users/thaiyoshi/.gem/ruby/1.8/gems/activesupport-2.3.2/lib/active_support/dependencies.rb:105:in `const_missing': uninitialized constant ActionController::Dispatcher::MiddlewareStack (NameError)
    from /Users/thaiyoshi/.gem/ruby/1.8/gems/actionpack-2.3.2/lib/action_controller/dispatcher.rb:71
    from /opt/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
    from /opt/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `require'
    from /Users/thaiyoshi/.gem/ruby/1.8/gems/activesupport-2.3.2/lib/active_support/dependencies.rb:156:in `require'
    from /Users/thaiyoshi/.gem/ruby/1.8/gems/activesupport-2.3.2/lib/active_support/dependencies.rb:521:in `new_constants_in'
    from /Users/thaiyoshi/.gem/ruby/1.8/gems/activesupport-2.3.2/lib/active_support/dependencies.rb:156:in `require'
    from /Users/thaiyoshi/.gem/ruby/1.8/gems/has_many_polymorphs-2.13/lib/has_many_polymorphs/autoload.rb:2
    from /opt/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
     ... 8 levels...
    from ./test/unit/../test_helper.rb:2:in `require'
    from ./test/unit/../test_helper.rb:2
    from test/unit/user_test.rb:1:in `require'
    from test/unit/user_test.rb:1

Here's my environment.rb file (default comments removed):

RAILS_GEM_VERSION = '2.3.2' unless defined? RAILS_GEM_VERSION
# config.gem 'has_many_polymorphs', :version => "2.12"
# require ‘has_many_polymorphs‘

# Bootstrap the Rails environment, frameworks, and default configuration

require File.join(File.dirname(__FILE__), 'boot')
require 'has_many_polymorphs' 
require 'active_support'

Rails::Initializer.run do |config|

  config.time_zone = 'UTC'


  config.gem "thoughtbot-factory_girl",
             :lib    => "factory_girl",
             :source => "http://gems.github.com"


   config.gem 'thoughtbot-shoulda',
              :lib => false,
              :source => 'http://gems.github.com'

end
+1  A: 

Hi, As I can see it is very fresh problem. I have it too. I followed has_many_polymorphs reame and after adding require to my environment I have the same situation. I will be glad for any reply.

+2  A: 

I previously wrote some misguided information, however, now I've managed to sit down and look at it undisturbed for a few minutes, I fixed my problem by editing my environment.rb, and moving the require 'has_many_polymorphs' statement to after the initializer block.

Bingo. I face-palmed hard, but this is how we learn..

Ric8ard
This worked for me, even though it is contrary to the has_many_polymorphs documentation.
Bryan Ward
+1  A: 

Been following this thread for a while - ric's answer above worked for us in development but switching to production mode this caused an error. Putting the require back above the initializer block and using Ric's original fix worked fine. His edit has removed it - so for anyone following behind, here it is again: BTW - we are now using trunk on hmp rather than the johnsbrn branch and that seems to work fine with 2.3.2 now

edit: It almost fixed the issue for me. I found afterwards that migrations broke with a similar issue. After looking through code, I quickly hacked up an answer, which involved requiring the missing class (action_controller/middleware_stack).

The quick & filthy fix is to edit the file under the relative path:

"johnsbrn-has_many_polymorphs/lib/has_many_polymorphs/autoload.rb"

and insert the following line between the 1st two require lines:

require 'action_controller/middleware_stack' unless defined? ::ActionController::MiddlewareStack

e.g. It ends up looking like this:

require 'initializer' unless defined? ::Rails::Initializer

require 'action_controller/middleware_stack' unless defined? ::ActionController::MiddlewareStack

require 'action_controller/dispatcher' unless defined? ::ActionController::Dispatcher

and now things work ok.

oops - Sorry about that. Had not got round to trying it in production mode yet (long dev cycle!)
Ric8ard
A: 

in environment.rb:

require File.join(File.dirname(__FILE__), 'boot')
require 'action_controller/middleware_stack' unless defined? ::ActionController::MiddlewareStack
require 'has_many_polymorphs'

works for me

ehaselwanter
A: 

In case anyone else browses to this page like i did, i solved this simply by upgrading my has_many_polymorphs gem from 1.12.1 to 1.13 and re-freezing it into my vendor folder (and updating the config.gem line in environment.rb to use the new version number).

Max Williams