Use Yaml for MongoMapper Config

后端 未结 1 1109
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-21 02:24

I have my Yaml configuration file, mongo.yml:

development:
  adapter: mongodb
  database: fhsclock_development
  host: localhost
  port: nil

te         


        
相关标签:
1条回答
  • 2020-12-21 02:47

    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'] %>
    
    0 讨论(0)
提交回复
热议问题