2013-03-12 42 views
9

我創建了一個Rails Mountable App,並添加了'mongoid'和'rspec'gem's。如果我嘗試運行我的規格現在我得到以下錯誤:Rails Engine + Mongoid:在默認的會話中找不到配置

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' 
+0

您可以發佈您mongoid.yml文件? – 2013-03-12 15:30:27

回答

19

你可能在你的spec_helper.rb文件無緣require 'rails/mongoid'

具有包含人同樣的問題在這裏https://github.com/mongoid/mongoid/issues/2894#issuecomment-14903927

嘗試添加要求,這應該修復它。

+0

我在哪裏可以找到'spec_helper.rb'文件? – 2013-12-13 18:13:18

+0

我在mongoid + rails的'Echo'示例項目中找到了,我沒有在我生成的應用程序文件夾中的'spec'文件夾 – 2013-12-13 18:18:19

+2

spec_helper.rb由rspec生成,如果你不使用rspec,在你的application.rb上 – 2014-10-08 13:28:43

2

,並重新啓動進行更改後的服務器mongoid.yml

4

這是可能是因爲兩個同步條件:(存在mongoid.yml沒有生產部分)AND(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 
0

我發現這個工作 - 公告不存在「會話」,只有「客戶」

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

這爲我工作我的機器

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

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

2:並改變你的mongoid。從陽明海運例如(僅適用於mongoid版本< 5):

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