Mac + Rails3 + MongoDB的Demo工程搭建

<!--?xml version="1.0" encoding="UTF-8" standalone="no"?-->

环境:
ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-darwin11.4.0]
rvm 1.14.5 (stable) by Wayne E. Seguin <[email protected]>, Michal Papis <[email protected]> [https://rvm.io/]
Rails 3.2.6
MongoDB db version v2.0.6, pdfile version 4.5


终端中执行命令:

rails new mongodemo
gem install mongo_mapper

在enviroment.rb中添加config.gem "mongo_mapper"
如启动报config变量无法找到,则修改config.gem "mongo_mapper"为$config.gem "mongo_mapper"

异常信息:
/Users/seraph/Documents/rails_projects/mongodemo/config/environment.rb:7:in `<top (required)>': undefined local variable or method `config' for main:Object (NameError)

$config代表全局变量引用

在config/initalizers中添加文件mongo_config.rb
内容:MongoMapper.database = "mongodemo-#{Rails.env}"

在Gemfile中添加
source "http://gemcutter.org"

gem 'mongo_mapper', github: "jnunemaker/mongomapper"
gem "bson_ext"
gem "nifty-generators", :group => :development
然后执行命令:bundle install

使用脚手架,生成MongoDB演示代码:
rails generate scaffold project name:string --orm=mongo_mapper
rails generate scaffold task project_id:string name:string completed:boolean --orm=mongo_mapper


访问MongoDB: http://localhost:28017/


各种异常情况处理:

1.问题:启动Rails服务器如报如下异常,说明没有安装bson_ext.

  gem install bson_ext

  If you continue to receive this message after installing, make sure that the
  bson_ext gem is in your load path and that the bson_ext and mongo gems are of the same version.

2.问题:在使用nifty生成代码时,如报如下异常,需执行:gem install nifty-generators
rails g nifty:scaffold Recipe name:string index new

/Users/seraph/.rvm/gems/ruby-1.9.3-p194@global/gems/bundler-1.1.4/lib/bundler/resolver.rb:287:in `resolve': Could not find gem 'nifty-generators (>= 0) ruby' in the gems available on this machine. (Bundler::GemNotFound)

3.问题:mongodb演示,访问http://localhost:3000/projects时,报异常:undefined method `key?' for nil:NilClass
解决:
(1)在Gemfile中添加
(2)在终端中运行 bundle install
解释:
Explanation: Rails 3.2.4 added a accessible_attributes method to ActiveModel, but MongoMapper already had this; so they were clobbering each other.

4.问题:访问http://localhost:3000/projects时,报如下异常:
ArgumentError (wrong number of arguments (1 for 0)):
  app/controllers/projects_controller.rb:1:in `<top (required)>'

解决办法:未能明确,怀疑

5.问题:如果访问MongoDBhttp://localhost:28017/页面时,页面提示需开启rest,则在/usr/local/mongodb/mongod.conf文件中添加
rest = true
全部内容是:
# Store data alongside MongoDB instead of the default, /data/db/
dbpath = /usr/local/mongodb_data

# Only accept local connections
bind_ip = 127.0.0.1

# Seraph add, to enable REST
rest = true



资源:


猜你喜欢

转载自seraph115.iteye.com/blog/1601242