1

我正在尝试用rails测试mongoDB,我对两者都很陌生。

首先,安装了 mongoDB 并作为 Windows 服务运行。

现在,这是我创建一个简单应用程序的步骤:

我用这个创建了应用程序:

rails new MongoOnRailsBlog -O -J -T

然后我修改了 gemfile,它现在看起来像这样:

source 'https://rubygems.org'

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.0.0'

gem 'mongoid'
gem 'bson_ext'
gem 'will_paginate'
gem 'devise'
gem 'haml'
gem 'haml-rails', :group => :development
gem 'hpricot', :group => :development
gem 'formtastic'
gem 'jquery-rails'

# Use SCSS for stylesheets
gem 'sass-rails', '~> 4.0.0'

# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .js.coffee assets and views
gem 'coffee-rails', '~> 4.0.0'

# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 1.2'

group :doc do
  # bundle exec rake doc:rails generates the API under doc/api.
  gem 'sdoc', require: false
end

我修改了 application.rb 以添加一些生成器:

require File.expand_path('../boot', __FILE__)

require "action_controller/railtie"
require "action_mailer/railtie"
require "sprockets/railtie"

# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(:default, Rails.env)

module MongoOnRailsBlog
  class Application < Rails::Application

    # Configure generators  
    config.generators do |g|  
      g.orm :mongoid  
      g.template_engine :haml  
    end  
  end
end

然后,当我尝试生成 mongoID 配置时,使用以下命令:

rails generate mongoid:config

我有“无法加载文件错误”

C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:228:in `require': cannot load such file -- mongoid/version (LoadError)
    from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:228:in `block in require'
    from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:213:in `load_dependency'
    from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:228:in `require'
    from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/devise-1.5.4/lib/devise/rails.rb:34:in `block in <class:Engine>'
    from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/railties-4.0.0/lib/rails/initializable.rb:30:in `instance_exec'
    from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/railties-4.0.0/lib/rails/initializable.rb:30:in `run'
    from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/railties-4.0.0/lib/rails/initializable.rb:55:in `block in run_initializers'
    from C:/Ruby200-x64/lib/ruby/2.0.0/tsort.rb:150:in `block in tsort_each'
    from C:/Ruby200-x64/lib/ruby/2.0.0/tsort.rb:183:in `block (2 levels) ineach_strongly_connected_component'
    from C:/Ruby200-x64/lib/ruby/2.0.0/tsort.rb:219:in `each_strongly_connected_component_from'
    from C:/Ruby200-x64/lib/ruby/2.0.0/tsort.rb:182:in `block in each_strongly_connected_component'
    from C:/Ruby200-x64/lib/ruby/2.0.0/tsort.rb:180:in `each'
    from C:/Ruby200-x64/lib/ruby/2.0.0/tsort.rb:180:in `each_strongly_connected_component'
    from C:/Ruby200-x64/lib/ruby/2.0.0/tsort.rb:148:in `tsort_each'
    from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/railties-4.0.0/lib/rails/initializable.rb:54:in `run_initializers'
    from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/railties-4.0.0/lib/rails/application.rb:215:in `initialize!'
    from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/railties-4.0.0/lib/rails/railtie/configurable.rb:30:in `method_missing'
    from E:/Dev/Projects/MongoOnRailsBlog/config/environment.rb:5:in `<top (required)>'
    from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:228:in `require'
    from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:228:in `block in require'
    from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:213:in `load_dependency'
    from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:228:in `require'
    from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/railties-4.0.0/lib/rails/application.rb:189:in `require_environment!'
    from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/railties-4.0.0/lib/rails/commands.rb:45:in `<top (required)>'
    from bin/rails:4:in `require'
    from bin/rails:4:in `<main>'

我猜它与 MongoID 的好版本有关,但我不知道在哪里验证,如何确保它加载好的版本......

关于在哪里看的任何建议?

感谢。

4

2 回答 2

0

现在,放入您的 Gemfile:

gem 'mongoid', github: 'mongoid/mongoid'
于 2013-09-01T04:11:46.367 回答
0

对于未来的问题,请提供 Rails 和 Mongoid 的确切版本信息。如果您查看 Gemfile.lock,很可能是捆绑器在 mongoid 版本 3.1.4 中拉取。如果您查看 Github 并比较标签 v3.1.4 与分支 master 的提交,很明显 v3.1.4 中不包含 Rails 4 支持。用于 Rails 4 的 Mongoid 官方版本仍在等待中。

所以此时,我建议您重试 Rails 3(当前为 3.2.13)以获得良好的成功。

于 2013-07-08T22:32:04.060 回答