Rails3.1 使用mongodb学习笔记之mongo_maper
MongoDB  /  houtizong 发布于 3年前   248
gem install rails
rails new project_name --skip-active-record
# require "rails/all" # 删除掉# 添加下面require "action_controller/railtie"require "action_mailer/railtie"require "active_resource/railtie"require "rails/test_unit/railtie"
# Configure generators values. Many other options are available, be sure to check the documentation.# config.generators do |g|# g.orm :active_record# g.template_engine :erb# g.test_framework :test_unit, :fixture => true# end
source 'http://gemcutter.org'require 'rubygems'require 'mongo'gem "rails", "3.1.0"gem "mongo_mapper"
bundle install
gem install bundle
MongoMapper.connection = Mongo::Connection.new('localhost', 27017)MongoMapper.database = "project_name_#{Rails.env}"if defined?(PhusionPassenger) PhusionPassenger.on_event(:starting_worker_process) do |forked| MongoMapper.connection.connect if forked endend
namespace :db do namespace :test do task :prepare do # Stub out for MongoDB end endend
**Notice: C extension not loaded. This is required for optimum MongoDB Ruby driver performance. You can install the extension as follows: 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.gems/ruby-1.9.2-p180/gems/execjs-1.3.0/lib/execjs/runtimes.rb:50:in `autodetect': Could not find a JavaScript runtime. See https://github.com/sstephenson/execjs for a list of available runtimes. (ExecJS::RuntimeUnavailable)from /home/chinacheng/.rvm/gems/ruby-1.9.2-p180/gems/execjs-1.3.0/lib/execjs.rb:5:in `<module:ExecJS>'from /home/chinacheng/.rvm/gems/ruby-1.9.2-p180/gems/execjs-1.3.0/lib/execjs.rb:4:in `<top (required)>'
gem install bson_ext
gem "bson_ext"
# Gems used only for assets and not required# in production environments by default.#group :assets do# gem 'sass-rails', " ~> 3.1.0"# gem 'coffee-rails', "~> 3.1.0"# gem 'uglifier'#end
gem 'therubyracer'gem 'execjs'
syntax error, unexpected ':', expecting kEND (SyntaxError) wrap_parameters format: [:json]
wrap_parameters :format=>[:json]
syntax error, unexpected ':', expecting $end (SyntaxError)...ion_store :cookie_store, key: '_project_name_session'
h = { a: 1, b: 2 }#
h = { :a=> 1, :b=>2 }# 返回{:a=>1, :b=>2}
rails g controller index
require 'test_helper' class IndexControllerTest < ActionController::TestCase test "access the index page" do get :index assert_response 200 end end
root :to=>'index#index'
class IndexController < ApplicationController def index end end
test_helper.rb:3:in `<class:TestCase>': undefined method `fixtures' for ActiveSupport::TestCase:Class (NoMethodError)
# fixtures :all
IndexControllerTest PASS test_access_the_index_page (0:00:00.334)Finished in 0.335200 seconds.1 tests, 1 passed, 0 failures, 0 errors, 0 skips, 1 assertions
rails g model topic --skip-migration --orm=mongo_mapper
No value provided for required options '--orm'
invoke mongo_mapper create app/models/user.rb invoke test_unit create test/unit/user_test.rb create test/fixtures/users.yml
config.generators do |g| g.orm :mongo_mapperend
class User include MongoMapper::Document key :name, String key :age, Integerend
class UserTest < ActiveSupport::TestCase test "new user test" do assert_equal User.count,0 assert_difference "User.count",1 do User.create(:name=>"王华",:age=>18) end user = User.last assert_equal user.name, "王华" assert_equal user.age, 18 end
** Invoke test:units (first_time)** Invoke test:prepare (first_time)** Invoke db:test:prepare (first_time)** Invoke environment (first_time)** Execute environment** Execute db:test:preparerake aborted!Set config before connecting. MongoMapper.config = {...}
project_name$ script/rails generate mongo_mapper:config create config/mongo.yml
development: host: 127.0.0.1 port: 27017 database: project_name_developmenttest: host: 127.0.0.1 port: 27017 database: project_name_test# set these environment variables on your prod serverproduction: host: 127.0.0.1 port: 27017 database: project_name username: <%= ENV['MONGO_USERNAME'] %> password: <%= ENV['MONGO_PASSWORD'] %>
UserTest PASS test_new_user_test (0:00:00.333)Finished in 0.469750 seconds.1 tests, 1 passed, 0 failures, 0 errors, 0 skips, 4 assertions
请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!
技术博客集 - 网站简介:
前后端技术:
后端基于Hyperf2.1框架开发,前端使用Bootstrap可视化布局系统生成
网站主要作用:
1.编程技术分享及讨论交流,内置聊天系统;
2.测试交流框架问题,比如:Hyperf、Laravel、TP、beego;
3.本站数据是基于大数据采集等爬虫技术为基础助力分享知识,如有侵权请发邮件到站长邮箱,站长会尽快处理;
4.站长邮箱:[email protected];
文章归档
文章标签
友情链接