Rails,Mongoid,为数据库配置使用环境变量抛出 NoMethodError

Rails, Mongoid, using environment variable for database config throws NoMethodError

我正在尝试将使用 mongoid 的 rails 应用程序部署到我的远程生产服务器。

在我的 mongoid.yml 我添加了这个:

hosts:
    - <%= ENV['MONGOSERVER_PORT_27017_TCP_ADDR'] %>:27017

当我启动我的 capistrano 时,它抛出了这个错误:

SSHKit::Command::Failed: rake exit status: 1
rake stdout: rake aborted!
NoMethodError: undefined method `split' for :"27017":Symbol

添加下划线会导致此错误吗?

环境变量好像是nil,所以直接读取:

hosts:
    - :27017

它认为宿主是一个符号,这是在扔掉它。

仔细检查您的 env var 设置是否正确,并尝试在主机周围加上引号:

hosts:
    - "<%= ENV['MONGOSERVER_PORT_27017_TCP_ADDR'] %>:27017"

该行被解析为 - :27017,这将创建一个 Symbol 而不是您的 URL 字符串。这可能是因为您的 ENV 变量没有被定义;确保您已正确定义它。

回答问题"does adding underscores cause this error?":很可能不会。下划线在环境变量中有效。