gpt4 book ai didi

mongodb - 未找到 Mongoid session

转载 作者:可可西里 更新时间:2023-11-01 09:17:27 24 4
gpt4 key购买 nike

试用 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 文件已加载
  • 散列包含正确的环境和数据库名称

使用 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 

最佳答案

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

关于mongodb - 未找到 Mongoid session ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13463969/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com