mongoid yaml组合主机的环境变量

Zen*_*aro 4 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
Run Code Online (Sandbox Code Playgroud)

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

我已经尝试过各种方法

    hosts:
      - <%= \"#{ENV['OPENSHIFT_MONGODB_HOST']}:#{ENV['OPENSHIFT_MONGODB_PORT']}\" %>
Run Code Online (Sandbox Code Playgroud)

要么

    hosts:
      - #{ENV['OPENSHIFT_MONGODB_HOST']:ENV['OPENSHIFT_MONGODB_PORT']}
Run Code Online (Sandbox Code Playgroud)

等等,但是没有用

Hak*_*ton 5

在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的蒙古项目中,我正在使用如下uri:字段:

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

还请注意缩进,它必须准确且必须空间!Tab也会引起问题!