4

试用 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'

我已经确认了以下内容:

  • 加载 Mongoid.yml 文件
  • 哈希包含正确的环境和数据库名称

使用 pryMongoid.load!方法的返回值返回:

=> {"sessions"=>
 {"default"=>
 {"database"=>"bluster",
 "hosts"=>["localhost:27017"],
 "options"=>{"consistency"=>"strong "}}}}

如果有任何帮助检查,我也添加了 app.rb 文件和 mongoid.yml 文件。

应用程序.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 
4

1 回答 1

1
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 文件,它工作正常。

于 2013-01-01T23:03:35.203 回答