开发者

Rails with Mongo Mapper

This is turning out to be the worst day for learning rails. Already failed to connect and configure to sqlite3, mysql databases I turned to mongo mapper and facing the same issues. When I try to start the rails server I get the following:

/usr/local/lib/ruby/gems/1.8/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:239:in `require': no such file to load -- mongo_mapper (LoadError)

I have already installed mongo_mapper gem but when I do 'which mongo_mapper' it does not return anything.

Here is my mongo_config.rb file placed inside the initializers folder:

require "rubygems"
require "mongo_mapper"

MongoMapper.database = "blog-#{Rails.env}"

UPDATE 1:

I changed to the following:

require "rubygems"
include MongoMapper

MongoMapper.database = "blog-#{Rails.env}"

And now I get the following error:

   /Users/azamsharp/Projects/railsprojects/blog/config/initializers/mongo_config.rb:2: uninitialized constant MongoMapper (Name开发者_JAVA技巧Error)


You must use bundler with rails 3. don't try to not use it. add the following to your Gemfile

source 'http://gemcutter.org'
source 'http://rubygems.org'

gem "rails", "3.0.7"
gem 'bson', "1.1.1"
gem 'bson_ext', "1.1.1"
gem 'mongo_mapper'

then run

bundle install

then rails server will work


In order to get mongo_mapper working in Rails (tested under Rails 3.1, should work in Rails 3.0.x as well), you have to follow these steps:

  1. In Gemfile, add the mongo_mapper and bson gem:

    gem 'mongo_mapper'
    gem 'bson_ext'
    
  2. Run bundle install

  3. In the config/ directory, create a mongo.yml file like this one:

    # config/mongo.yml
    
    defaults: &defaults
      host: 127.0.0.1
      port: 27017
    
    development:
      <<: *defaults
      database: db_development
      username: user
      password: password
    
    test:
      <<: *defaults
      database: db_test
    
    # set these environment variables on your prod server
    production:
      <<: *defaults
      database:
      username:
      password:
    
  4. In the config/initializers directory, create a mongo.rb file like this:

     # config/initializers/mongo.rb
    
     #include MongoMapper
    
     db_config = YAML::load(File.read(File.join(Rails.root, "/config/mongo.yml")))
     #
     if db_config[Rails.env] && db_config[Rails.env]['adapter'] == 'mongodb'
       mongo = db_config[Rails.env]
       MongoMapper.connection = Mongo::Connection.new(mongo['host'] || 'localhost',
                                                  mongo['port'] || 27017,
                                                 :logger => Rails.logger)
       MongoMapper.database = mongo['database']
    
       if mongo['username'] && mongo['password']
         MongoMapper.database.authenticate(mongo['username'], mongo['password'])
       end
     end
    
     # Used for image uploads
     # CarrierWave.configure do |config|
     #   mongo = db_config[Rails.env]
     #   config.grid_fs_database = mongo['database']
     #   config.grid_fs_host = mongo['host'] || 'localhost'
     #   config.grid_fs_access_url = "gridfs"
     #   config.grid_fs_username = mongo['username']
     #   config.grid_fs_password =  mongo['password']
     # end
    
     # It's also possible to define indexes in the the model itself; however,
     # a few issues are being worked out still. This is a temporary solution.
     # Comment.ensure_index([["story_id", 1], ["path", 1], ["points", -1]])
     # MongoMapper.ensure_indexes!
    
     # Handle passenger forking.
     # if defined?(PhusionPassenger)
     #    PhusionPassenger.on_event(:starting_worker_process) do |forked|
     #      MongoMapper.database.connect_to_master if forked
     #    end
     # end
    
     if defined?(PhusionPassenger)
        PhusionPassenger.on_event(:starting_worker_process) do |forked|
          MongoMapper.connection.connect if forked
        end
     end
    
  5. Start the mongod server before starting the rails server and enjoy!


Try removing the require lines and replace them with include MongoMapper

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜