【问题标题】:Rails Engine + Mongoid: No configuration could be found for a session named 'default'Rails Engine + Mongoid:找不到名为“默认”的会话的配置
【发布时间】:2013-02-27 14:16:21
【问题描述】:

我创建了一个 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.

当我将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

版本:

gem 'rails', '~> 3.2.12'
gem 'mongoid', '~> 3.1'
gem 'rspec-rails', '~> 2.13'

【问题讨论】:

  • 你能发布你的 mongoid.yml 文件吗?

标签: rails-engines mongoid3


【解决方案1】:

尝试重新生成一个新的mongoid.yml

rails g mongoid:config

然后用你的价值观改变mongoid.yml

【讨论】:

    【解决方案2】:

    这在我的机器上对我有用

    1:将其添加到您的 config/application.rb

    Mongoid.load!("path to your mongoid.yml")
    

    2:然后将您的 mongoid.yml 更改为(仅适用于 mongoid 版本

    这个

    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
    

    收件人:

    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
    

    【讨论】:

    • Rails 4.2.7.1 在安装和配置 mongoid 时在定位/理解 mongoid.yml 文件时出现问题。 mongoid.yml 文件已成功生成但未被识别。 #1 为我工作。
    【解决方案3】:

    我发现这个工作 - 注意没有“会话”,只有“客户”

    production:
      clients:
        default:
          uri: <%= ENV['MONGODB_URI'] %>
          options:
            skip_version_check: true
            safe: true
    

    【讨论】:

      【解决方案4】:

      这可能是因为两个同时发生的条件:(在 mongoid.yml 中没有生产部分)和(Heroku 默认将 Rails 应用程序视为生产)。

      修复其中一个就足够了。

      1. mongoid.yml 中没有生产部分

      将生产部分添加到 mongoid.yml,如 Heroku 中所述,例如

      production:
        sessions:
          default:
            uri: <%= ENV['MONGOHQ_URL'] %>
            options:
              skip_version_check: true
              safe: true
      

      2。 Heroku 默认将 Rails 应用程序视为生产环境

      将 Heroku 环境设置为开发环境,或添加特定于 Heroku 的新环境,如 Heroku 中所述,例如

      heroku config:set RACK_ENV=development RAILS_ENV=development --remote development
      

      【讨论】:

        【解决方案5】:

        修改mongoid.yml后重启服务器

        【讨论】:

          【解决方案6】:

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

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

          尝试添加该要求,应该可以解决它。

          【讨论】:

          • 在哪里可以找到spec_helper.rb 文件?
          • 我在 Echo 的 mongoid+rails 示例项目中找到,而我生成的应用文件夹中没有 spec 文件夹
          • spec_helper.rb 是 rspec 生成的,如果你不使用 rspec,把它放在你的 application.rb 中
          猜你喜欢
          • 2013-07-08
          • 1970-01-01
          • 2016-10-17
          • 2015-03-31
          • 1970-01-01
          • 1970-01-01
          • 2016-06-14
          • 1970-01-01
          • 2015-09-29
          相关资源
          最近更新 更多