我在heroku上有一个Sinatra应用程序,由于这个错误它一直崩溃:
app/vendor/bundle/ruby/1.9.1/gems/mongoid-1.2.14/lib/mongoid/config.rb:52 in 'master': Database should be a Mongo::DB, not a nil class
我根据heroku指令设置了Mongoid 3.x,该应用程序在我的本地机器上工作,所以我不确定是什么原因导致了这个问题。我的gemfile看起来像这样:
source "https://rubygems.org"
ruby "1.9.3"
gem 'sinatra'
gem 'mongo'
gem 'mongoid'
gem 'bson_ext'
gem 'json'
gem 'nokogiri'
gem 'aws-s3', '0.6.2', :require => 'aws/s3'
gem 'sinatra-reloader'
gem 'debugger'
gem 'thin'
这是我的mongoid.yml
:
development:
sessions:
default:
database: db
hosts:
- localhost:27017
production:
sessions:
default:
uri: <%= ENV['MONGOHQ_URL'] %>
options:
skip_version_check: true
safe: true
这是我的app文件:
require 'bundler/setup'
require 'sinatra'
require 'json'
require 'mongo'
require 'mongoid'
Mongoid.load!('mongoid.yml', :production)
def get_connection
return @db_connection if @db_connection
db = URI.parse(ENV['MONGOHQ_URL'])
db_name = db.path.gsub(/^\//, '')
@db_connection = Mongo::Connection.new(db.host, db.port).db(db_name)
@db_connection.authenticate(db.user, db.password) unless (db.user.nil? || db.user.nil?)
@db_connection
end
db = get_connection
class Model1
include Mongoid::Document
field :name, :type => String
end
我不必指定数据库名称因为我正在使用uri字段,所以我不确定为什么数据库是否为nil?