I am trying to include array-of-hash data in a chef databag item. Is this supported? I keep getting error messages about an undefined method. Here's an example:
Databag item:
{
"id": "BurgerKing",
"sandwich": "Whopper",
"nickname": "BK",
"value_meals":[
{
"sandwich": "Big King",
"side": "Fries",
"drink": "Coke"
},
{
"sandwich": "Kids Burger",
"side": "Apple Slices",
"drink": "Milk"
}
]
}
Template:
<!-- 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 -%>
If I only access the sandwich and nickname fields, I'm great, but as soon as I add code to iterate through the value_meals, I get:
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 -%>
Thanks ahead!!!
And thanks CodeRanger!!!
Adding the below, which fixed it here, since I apparently don't have the knack for formatting comments yet.
<!-- But there are also value meals... -->
<% @restaurant['value_meals'].each do | meal | %>
<!-- <%= meal[ 'sandwich' ] %>, <%= meal[ 'side' ] %>, and <%= meal[ 'drink' ] %> -->
<% end -%>
The way you access hashes in Ruby is via []
syntax, so @restaurant['value_meals'].each
and meal['side']
.