当尝试索引div以便折叠按钮将正确折叠时,它会将我的json文件中每个类别中的每个第一个联系人编入索引为0,而不是连续索引每个联系人,无论是连续的类别。
这是我的erb文件:
<% @contacts.each do |category, hash| %>
<div class="panel-group" id="accordion">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title"><%= category %></h3>
</div>
<div class="panel-body">
<ul class="list-group">
<% hash.each_with_index do |contact, index| %>
<li class="list-group-item" style="border: none">
<button type="button" class="btn btn-info" data-toggle="collapse" data-parent="#accordion" data-target="#collapse<%= index %>"><%= contact['name'] %><%= index %></button>
<div id="collapse<%= index %>" class="panel-collapse collapse">
<%= contact['email'] %></br><%= contact['ext'] %>
</div>
</li>
<% end %>
</ul>
</div>
</div>
</div>
<% end %>
json文件:(它将每个组中的名字索引,即“经理,团队负责人”为0.我希望Rob = 0,Terry = 1,Ben = 3,依此类推。
{
"Manager":[
{
"name": "Rob",
"email": "Rob@com",
"ext": "ext:"
}
],
"Team Lead":[
{
"name": "Terry",
"email": "Terry@com",
"ext": "ext"
}
],
"Unix Admins":[
{
"name": "Ben",
"email": "Benjamin@com",
"ext": "ext"
},
{
"name": "Prashant",
"email": "Prashant@com",
"ext": "ext"
},
{
"name": "Harshil
"email": "Harshil@com",
"ext": "ext"
},
{
"name": "Laxmikanth",
"email": "Laxmikanth@com",
"ext": "ext"
},
{
"name": "Mary",
"email": "Mary@com",
"ext": "ext"
}
],
"On-Call Contact":[
{
"name": "SYSTEM ADMIN UNIX",
"email": "Unix@com",
"ext": "ext"
}
]
}
.rb文件:
get '/contact' do
contact = Contacts.new
@contacts = contact.getContacts
@contacts.each { |s| puts "get /contact found contact #{s.last.first['name']}" }
erb :contact