我有嵌套数组:
book= {"Dan Brown"=>["Angels and Demons", "The Da Vinci Code"], "Dale Carnegie"=>["How to Win Friends and Influence People", "How to Stop Worrying and Start Living"]}
我需要这个html结果:
<h3 class="title">Book</h3>
<h4>Dan Brown</h4>
<ul>
<li>Angels and Demons</li>
<li>The Da Vinci Code</li>
</ul>
<h4>Dale Carnegie</h4>
<ul>
<li>How to Win Friends and Influence People</li>
<li>How to Stop Worrying and Start Living</li>
</ul>
无法通过erb模板了解如何操作。
可能来自这种方法:
book.each {|key, value| puts "#{key} is #{value}" }
但对我来说这种方法不起作用,我想我无法理解如何使用它。 谢谢你的帮助。
答案 0 :(得分:1)
<h3 class="title">Book</h3>
<% book.each do |author,books| %>
<h4><%= author%></h4>
<ul>
<% books.each do |book| %>
<li><%= book%></li>
<% end %>
</ul>
<% end %>
答案 1 :(得分:1)
就像那样:
<h3 class="title">Book</h3>
<% book.each do |key, value| %>
<h4><%= key %></h4>
<ul>
<% value.each do |title| %>
<li><%= title %></li>
<% end %>
</ul>
<% end %>