Use Yaml for MongoMapper Config

岁酱吖の 提交于 2019-11-29 14:41:42

MongoMapper will just use the file if it's you're using Rails and the file is at config/mongo.yml. If you're not on Rails, you can adapt this code from the source:

config_file = Rails.root.join('config/mongo.yml')
if config_file.file?
  config = YAML.load(ERB.new(config_file.read).result)
  MongoMapper.setup(config, Rails.env, :logger => Rails.logger)
end

Also, the "adapter" in your file is extraneous. (See the Getting Started documentation). A mongo.yml from rails g mongo_mapper:config looks like:

defaults: &defaults
  host: 127.0.0.1
  port: 27017

development:
  <<: *defaults
  database: my_app_development

test:
  <<: *defaults
  database: my_app_test

# set these environment variables on your prod server
production:
  <<: *defaults
  database: my_app
  username: <%= ENV['MONGO_USERNAME'] %>
  password: <%= ENV['MONGO_PASSWORD'] %>
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!