0

我有嵌套数组:

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}" }

但是对我来说这种方法不起作用,我想我无法理解如何使用它。谢谢你的帮助。

4

2 回答 2

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 %>
于 2013-10-15T12:29:18.520 回答
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 %>
于 2013-10-15T12:27:51.163 回答