rails g给出了未初始化的常量Rack :: Mongoid

时间:2011-08-02 19:23:41

标签: ruby-on-rails ruby ruby-on-rails-3 mongoid rack

当我运行'rails g'时,我收到此错误(我的应用似乎运行良好 否则,但我需要使用发电机):

      /home/love/mongodb.roger/.bundler/ruby/1.8/gems/activesupport-3.0.9/lib/active_support/inflector/methods.rb:113:in `constantize': uninitialized constant Rack::Mongoid (NameError)
           from /home/love/mongodb.roger/.bundler/ruby/1.8/gems/activesupport-3.0.9/lib/active_support/inflector/methods.rb:112:in `each'
           from /home/love/mongodb.roger/.bundler/ruby/1.8/gems/activesupport-3.0.9/lib/active_support/inflector/methods.rb:112:in `constantize'
           from /home/love/mongodb.roger/.bundler/ruby/1.8/gems/actionpack-3.0.9/lib/action_dispatch/middleware/stack.rb:14:in `klass'
           from /home/love/mongodb.roger/.bundler/ruby/1.8/gems/actionpack-3.0.9/lib/action_dispatch/middleware/stack.rb:33:in `build'
           from /home/love/mongodb.roger/.bundler/ruby/1.8/gems/actionpack-3.0.9/lib/action_dispatch/middleware/stack.rb:79:in `build'
           from /home/love/mongodb.roger/.bundler/ruby/1.8/gems/railties-3.0.9/lib/rails/application.rb:209:in `inject'
           from /home/love/mongodb.roger/.bundler/ruby/1.8/gems/actionpack-3.0.9/lib/action_dispatch/middleware/stack.rb:79:in `each'
           from /home/love/mongodb.roger/.bundler/ruby/1.8/gems/actionpack-3.0.9/lib/action_dispatch/middleware/stack.rb:79:in `inject'
           from /home/love/mongodb.roger/.bundler/ruby/1.8/gems/actionpack-3.0.9/lib/action_dispatch/middleware/stack.rb:79:in `build'
           from /home/love/mongodb.roger/.bundler/ruby/1.8/gems/railties-3.0.9/lib/rails/application.rb:162:in `build_middleware_stack'
           from /home/love/mongodb.roger/.bundler/ruby/1.8/gems/railties-3.0.9/lib/rails/application/finisher.rb:35
           from /home/love/mongodb.roger/.bundler/ruby/1.8/gems/railties-3.0.9/lib/rails/initializable.rb:25:in `instance_exec'
           from /home/love/mongodb.roger/.bundler/ruby/1.8/gems/railties-3.0.9/lib/rails/initializable.rb:25:in `run'
           from /home/love/mongodb.roger/.bundler/ruby/1.8/gems/railties-3.0.9/lib/rails/initializable.rb:50:in `run_initializers'
           from /home/love/mongodb.roger/.bundler/ruby/1.8/gems/railties-3.0.9/lib/rails/initializable.rb:49:in `each'
           from /home/love/mongodb.roger/.bundler/ruby/1.8/gems/railties-3.0.9/lib/rails/initializable.rb:49:in `run_initializers'
           from /home/love/mongodb.roger/.bundler/ruby/1.8/gems/railties-3.0.9/lib/rails/application.rb:134:in `initialize!'
           from /home/love/mongodb.roger/.bundler/ruby/1.8/gems/railties-3.0.9/lib/rails/application.rb:77:in `send'
           from /home/love/mongodb.roger/.bundler/ruby/1.8/gems/railties-3.0.9/lib/rails/application.rb:77:in `method_missing'
           from /home/love/mongodb.roger/config/environment.rb:7
           from /home/love/mongodb.roger/.bundler/ruby/1.8/gems/railties-3.0.9/lib/rails/application.rb:103:in `require_environment!'
           from /home/love/mongodb.roger/.bundler/ruby/1.8/gems/railties-3.0.9/lib/rails/commands.rb:16
           from script/rails:6:in `require'
           from script/rails:6

我使用1.8.7(但是当我使用1.9.x和RVM时我也会收到此错误)。

ruby -v 
ruby 1.8.7 (2010-08-16 patchlevel 302) [x86_64-linux] 

rails -v 
Rails 3.0.9 

cat Gemfile 
source 'http://rubygems.org' 
gem 'rails', '3.0.9' 
gem "mongoid", "~> 2.1.0" 
gem "bson_ext", "~> 1.3" 
gem "devise" #, :git=>"https://github.com/plataformatec/devise.git" 
gem "rails-footnotes", ">= 3.7", :group => :development 
gem "compass" 
gem 'SystemTimer' 
gem "oa-oauth", :require => "omniauth/oauth" 
gem "warden" 
gem 'formtastic', '~> 1.1.0' 
gem 'paperclip' 
gem 'jquery-rails' 
gem 'acts-as-taggable-on' 
gem "gmaps4rails" 
gem 'browser' 
gem 'apotomo', "~>1.1.0" 
gem 'sass' 
gem 'chunky_png' 
gem 'mongoid-paperclip' 

有没有人有任何想法?

2 个答案:

答案 0 :(得分:8)

config/application.rb文件中删除该行:

require "mongoid/railtie"

答案 1 :(得分:2)

在使用Mongoid时,我已经看到了从Rails 3.0.7迁移到Rails 3.1.3的这个问题。要解决这个问题,请找到你正在做的所有mongoid需要的地方 - 例如 - 在config / application.rb

require 'mongoid/railtie'
require 'action_controller/railtie'
require 'action_mailer/railtie'
require 'active_resource/railtie'
require 'rails/test_unit/railtie'

...然后在此之前添加require'chack / mongoid'给你......

require 'rack/mongoid' # TADA!
require 'mongoid/railtie'
require 'action_controller/railtie'
require 'action_mailer/railtie'
require 'active_resource/railtie'
require 'rails/test_unit/railtie'

这也让Rails控制台也为我工作(rails c)