在 database.yml 中使用 'boot2docker ip'?

Using 'boot2docker ip' in your database.yml?

我想在我的 database.yml 中使用如下内容:

host: %x(boot2docker ip)

但是,每当我这样做时,我都会返回类似以下错误的信息:

RuntimeError (YAML syntax error occurred while parsing ...-database.yml. Please note that YAML must be consistently indented using spaces. Tabs are not allowed. Error: (<unknown>): found character that cannot start any token while scanning for the next token at line 12 column 9):

我似乎很清楚,这是关于 boot2docker 响应 ip 的方式错误,添加了换行符或类似内容。我可以把它放在其他地方,但内联似乎是正确的 - 有没有一种优雅的(单行)方式来做到这一点?

也许您的 yaml 解析器需要双引号:

host: "%x(boot2docker ip)"

原来 database.yml 不是 ruby 文件,它只是原始的 yml,模板在其中起作用。如果我要执行ruby,我需要转义

这里的答案如下:

host: <%= %x(boot2docker ip) %>