0

我正在尝试在厨师模板中渲染一个 yaml 对象,它工作正常,但正如我们所知,在 yaml 空间中也很重要,因此它正在创建新属性。

collection: <%= @collection.to_hash.to_yaml %>

其中collection是一个json对象

collection = { "name": "col1", "nested": { "nKey": 1 } }

预期结果是

collection:
  name: col1
  nested:
    nKey: 1

得到结果

collection: ---
  name: col1
nested: !ruby/hash:Mash #nested is not part of collection.
  nKey: 1

其次,有什么方法可以从 to_yaml 函数中删除 types( !ruby/hash:Mash)吗?---

4

1 回答 1

0

您通常想使用to_json,因为所有 JSON 都是有效的 YAML。它有点难看,但通常效果更好:)

于 2018-07-26T07:01:00.257 回答