Mac+Rails 3+MongoDBのDemoエンジニアリング構築



環境:
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
ここにいる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 `': 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.
リンク:http://stackoverflow.com/questions/11000788/undefined-method-key-for-nilnilclass-when-using-mongomapper
4.質問:http://localhost:3000/projectsにアクセスすると、次のような例外が表示されます.
ArgumentError (wrong number of arguments (1 for 0)):
  app/controllers/projects_controller.rb:1:in `'
解決策:明確にできなかった、疑う
5.質問:MongoDB http://localhost:28017/ページにアクセスする際に、ページプロンプトでrestを開く必要がある場合は、/usr/local/mongodb/mongodb.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
Mac環境でのMongoDBのインストール:http://shiftcommathree.com/articles/how-to-install-mongodb-on-os-x