Rails Engine + Mongoid:找不到名为'default'的会话的配置

win*_*ons 10 rails-engines mongoid3

我已经创建了一个Rails可安装应用程序并添加了'mongoid'和'rspec'gem.如果我现在尝试运行我的规格,我会收到以下错误:

Mongoid::Errors::NoSessionConfig: 
Problem:
  No configuration could be found for a session named 'default'.
Summary:
  When attempting to create the new session, Mongoid could not find a session configuration for the name: 'default'. This is necessary in order to know the host, port, and options needed to connect.
Resolution:
  Double check your mongoid.yml to make sure under the sessions key that a configuration exists for 'default'. If you have set the configuration programatically, ensure that 'default' exists in the configuration hash.
Run Code Online (Sandbox Code Playgroud)

当我添加Mongoid.load!(Rails.root.join("config", "mongoid.yml"))线到spec_helper.rb一切正常.

为什么这样,我怎样才能获得像普通Rails应用程序中的功能,我不需要调用加载函数?

mongoid.yml

development:
  sessions:
    default:
      database: dummy_development
      hosts:
        - localhost:27017
      options:
  options:
test:
  sessions:
    default:
      database: dummy_test
      hosts:
        - localhost:27017
      options:
        consistency: :strong
        max_retries: 1
        retry_interval: 0
Run Code Online (Sandbox Code Playgroud)

版本:

gem 'rails', '~> 3.2.12'
gem 'mongoid', '~> 3.1'
gem 'rspec-rails', '~> 2.13'
Run Code Online (Sandbox Code Playgroud)

Art*_*ves 20

您可能错过require 'rails/mongoid'了spec_helper.rb文件.

如果有人在这里有同样的问题https://github.com/mongoid/mongoid/issues/2894#issuecomment-14903927

尝试添加需要的,应该修复它.

  • spec_helper.rb由rspec生成,如果您没有使用rspec,请将其放在您的application.rb上 (2认同)

Nik*_*kar 5

This worked for me on my machine

1: Add this to your config/application.rb

Mongoid.load!("path to your mongoid.yml")
Run Code Online (Sandbox Code Playgroud)

2: And change your mongoid.yml from (Only for mongoid version < 5):

This

development:
  clients:
    default:
      database: database_for_development
        hosts:
          - localhost:27017
test:
  clients:
    default:
      database: database_for_test
        hosts:
          - localhost:27017
production:
  clients:
    default:
      database: database_for_production
        hosts:
          - localhost:27017
Run Code Online (Sandbox Code Playgroud)

To:

development:
  sessions:
    default:
      database: database_for_development
        hosts:
          - localhost:27017
test:
  sessions:
    default:
      database: database_for_test
        hosts:
          - localhost:27017
production:
  sessions:
    default:
      database: database_for_production
        hosts:
          - localhost:27017
Run Code Online (Sandbox Code Playgroud)