尝试Sinatra | Mongoid 3.每当我尝试保存到数据库时,我都会遇到以下错误。
Mongoid::Errors::NoSessionsConfig:
Problem:
No sessions configuration provided.
Summary:
Mongoid's configuration requires that you provide details about each session that can be connected to, and requires in the sessions config at least 1 default session to exist.
Resolution:
Double check your mongoid.yml to make sure that you have a top-level sessions key with at least 1 default session configuration for it. You can regenerate a new mongoid.yml for assistance via `rails g mongoid:config`.
Example:
development:
sessions:
default:
database: mongoid_dev
hosts:
- localhost:27017
from /Users/rhodee/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/mongoid-3.0.13/lib/mongoid/sessions/factory.rb:61:in `create_session'
我已经确认了以下内容:
使用pry,Mongoid.load!
方法的返回值返回:
=> {"sessions"=>
{"default"=>
{"database"=>"bluster",
"hosts"=>["localhost:27017"],
"options"=>{"consistency"=>"strong "}}}}
如果有任何帮助检查,我也添加了app.rb文件和mongoid.yml文件。
App.rb
require 'sinatra'
require 'mongoid'
require 'pry'
require 'routes'
require 'location'
configure :development do
enable :logging, :dump_errors, :run, :sessions
Mongoid.load!(File.join(File.dirname(__FILE__), "config", "mongoid.yml"))
end
Mongoid.yml
development:
sessions:
default:
database: bluster
hosts:
- localhost:27017
options:
consistency: strong
答案 0 :(得分:0)
require 'sinatra'
require 'mongoid'
require 'pry'
require 'routes'
configure :development do
enable :logging, :dump_errors, :run, :sessions
Mongoid.load!(File.join(File.dirname(__FILE__), "config", "mongoid.yml"))
end
get '/db' do
"db: " << Mongoid.default_session[:moped].database.inspect
end
我把一个例子放在一起,它对我来说很好。可能你的问题是别的,比如配置文件没有读访问权或其他东西。无论如何,我的配置文件与你的相同,这是我的sinatra文件,它工作正常。