Chef 数据包中的哈希数组?
array of hash in Chef databags?
我正在尝试在 Chef 数据包项中包含哈希数组数据。这受支持吗?我不断收到有关未定义方法的错误消息。这是一个例子:
资料袋项目:
{
"id": "BurgerKing",
"sandwich": "Whopper",
"nickname": "BK",
"value_meals":[
{
"sandwich": "Big King",
"side": "Fries",
"drink": "Coke"
},
{
"sandwich": "Kids Burger",
"side": "Apple Slices",
"drink": "Milk"
}
]
}
模板:
<!-- I want to go to <%= @restaurant[ 'nickname' ] %> to get a <%= @restaurant['sandwich'] %>. -->
<!-- But there are also value meals... -->
<% @restaurant.value_meals.each do | meal | %>
<!-- <%= meal.sandwich %>, <%= meal.side %>, and <%= meal.drink %> -->
<% end -%>
如果我只访问三明治和昵称字段,那很好,但是一旦我添加代码以遍历 value_meals,我得到:
Chef::Mixin::Template::TemplateError (undefined method `value_meals' for #<Chef::DataBagItem:0x00000002d07df0>) on line #6:
4: <!-- I want to go to <%= @restaurant[ 'nickname' ] %> to get a <%= @restaurant['sandwich'] %>. -->
5: <!-- But there are also value meals... -->
6: <% @restaurant.value_meals.each do | meal | %>
7: <!-- <%= meal.sandwich %>, <%= meal.side %>, and <%= meal.drink %> -->
8: <% end -%>
提前致谢!!!
感谢 CodeRanger!!!
添加以下内容,修复了这里的问题,因为我显然还没有格式化评论的诀窍。
<!-- But there are also value meals... -->
<% @restaurant['value_meals'].each do | meal | %>
<!-- <%= meal[ 'sandwich' ] %>, <%= meal[ 'side' ] %>, and <%= meal[ 'drink' ] %> -->
<% end -%>
您在 Ruby 中访问哈希的方式是通过 []
语法,因此 @restaurant['value_meals'].each
和 meal['side']
.
我正在尝试在 Chef 数据包项中包含哈希数组数据。这受支持吗?我不断收到有关未定义方法的错误消息。这是一个例子:
资料袋项目:
{
"id": "BurgerKing",
"sandwich": "Whopper",
"nickname": "BK",
"value_meals":[
{
"sandwich": "Big King",
"side": "Fries",
"drink": "Coke"
},
{
"sandwich": "Kids Burger",
"side": "Apple Slices",
"drink": "Milk"
}
]
}
模板:
<!-- I want to go to <%= @restaurant[ 'nickname' ] %> to get a <%= @restaurant['sandwich'] %>. -->
<!-- But there are also value meals... -->
<% @restaurant.value_meals.each do | meal | %>
<!-- <%= meal.sandwich %>, <%= meal.side %>, and <%= meal.drink %> -->
<% end -%>
如果我只访问三明治和昵称字段,那很好,但是一旦我添加代码以遍历 value_meals,我得到:
Chef::Mixin::Template::TemplateError (undefined method `value_meals' for #<Chef::DataBagItem:0x00000002d07df0>) on line #6:
4: <!-- I want to go to <%= @restaurant[ 'nickname' ] %> to get a <%= @restaurant['sandwich'] %>. -->
5: <!-- But there are also value meals... -->
6: <% @restaurant.value_meals.each do | meal | %>
7: <!-- <%= meal.sandwich %>, <%= meal.side %>, and <%= meal.drink %> -->
8: <% end -%>
提前致谢!!!
感谢 CodeRanger!!!
添加以下内容,修复了这里的问题,因为我显然还没有格式化评论的诀窍。
<!-- But there are also value meals... -->
<% @restaurant['value_meals'].each do | meal | %>
<!-- <%= meal[ 'sandwich' ] %>, <%= meal[ 'side' ] %>, and <%= meal[ 'drink' ] %> -->
<% end -%>
您在 Ruby 中访问哈希的方式是通过 []
语法,因此 @restaurant['value_meals'].each
和 meal['side']
.