---
title: "Mac + Rails3 + MongoDB的Demo工程搭建"
author: "Seraph115"
date: "2012-07-19T18:20:27Z"
tags: []
categories: ["互联网"]
slug: "mac-rails3-mongodb-demo"
draft: false
lastmod: "2012-07-19T18:23:28Z"
id: 1601242
url: "https://seraph115.iteye.com/blog/1601242"
---

&lt;!--?xml version="1.0" encoding="UTF-8" standalone="no"?--&gt;

环境：

ruby 1.9.3p194 (2012-04-20 revision 35410) \[x86\_64-darwin11.4.0]

rvm 1.14.5 (stable) by Wayne E. Seguin &lt;[wayneeseguin@gmail.com](mailto:wayneeseguin@gmail.com)&gt;, Michal Papis &lt;[mpapis@gmail.com](mailto:mpapis@gmail.com)&gt; \[[https://rvm.io/](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 \`&lt;top (required)&gt;': 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](http://gemcutter.org/)"

gem 'mongo\_mapper', github: "jnunemaker/mongomapper"

gem "bson\_ext"

gem "nifty-generators", :group =&gt; :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/](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 (&gt;= 0) ruby' in the gems available on this machine. (Bundler::GemNotFound)

* * *

3.问题：mongodb演示，访问[http://localhost:3000/projects](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.

链接：[http://stackoverflow.com/questions/11000788/undefined-method-key-for-nilnilclass-when-using-mongomapper](http://stackoverflow.com/questions/11000788/undefined-method-key-for-nilnilclass-when-using-mongomapper)

* * *

4.问题：访问[http://localhost:3000/projects](http://localhost:3000/projects)时，报如下异常：

ArgumentError (wrong number of arguments (1 for 0)):

  app/controllers/projects\_controller.rb:1:in \`&lt;top (required)&gt;'

解决办法：未能明确，怀疑

* * *

5.问题：如果访问MongoDB[http://localhost:28017/](http://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

* * *

资源：

Rails + MongoDB的Demo开发: [http://railscasts.com/episodes/194-mongodb-and-mongomapper?autoplay=true](http://railscasts.com/episodes/194-mongodb-and-mongomapper?autoplay=true)

在Mac环境下安装MongoDB: [http://shiftcommathree.com/articles/how-to-install-mongodb-on-os-x](http://shiftcommathree.com/articles/how-to-install-mongodb-on-os-x)