mongoid yaml结合了主机

时间:2016-02-23 08:57:17

标签: ruby-on-rails environment-variables mongoid openshift

我是rails和mongoid的新手,我有mongoid.yml文件,其中包含以下条目:

development:
  # Configure available database clients. (required)
  clients:
    # Defines the default client. (required)
    default:
      # Defines the name of the default database that Mongoid can connect to.
      # (required).
      database: mycollectionname
      # Provides the hosts the default client can connect to. Must be an array
      # of host:port pairs. (required)
      hosts:
        - localhost:27017

这适用于开发,但是,在生产中,我想从环境变量中指定主机,例如ENV['OPENSHIFT_MONGODB_DB_HOST'] +“:”+ ENV['OPENSHIFT_MONGODB_DB_PORT']

我尝试了各种各样的方式,比如这个

    hosts:
      - <%= \"#{ENV['OPENSHIFT_MONGODB_HOST']}:#{ENV['OPENSHIFT_MONGODB_PORT']}\" %>

    hosts:
      - #{ENV['OPENSHIFT_MONGODB_HOST']:ENV['OPENSHIFT_MONGODB_PORT']}

等,但没有效果

1 个答案:

答案 0 :(得分:4)

在yaml代码中,<%= %>用于插入ruby代码,您可以在其中使用Expression Substitution来格式化您的网址

Expression substitution is a means of embedding the value of any Ruby expression into a string using #{ and }

这样的事情会:

<%= "#{ENV['OPENSHIFT_MONGODB_HOST']}:#{ENV['OPENSHIFT_MONGODB_PORT']}" %>

在我使用openshift的mongoid项目中,我正在使用它的uri:字段:

uri: <%= "#{ENV['OPENSHIFT_MONGODB_DB_URL']}#{ENV['OPENSHIFT_APP_NAME']}" %>

请注意压痕,它必须准确,它必须是空间! Tab也会引起问题!