Ragnar Lodbrog
Ragnar Lodbrog

Reputation: 21

erb template for nested array

I have nested array:

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

I need this html result:

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

Can't understand how to do it via erb template.

May be from this method:

book.each {|key, value| puts "#{key} is #{value}" }

But for me this method don't work, I think I can't understand how to use it. Thank you for help.

Upvotes: 0

Views: 914

Answers (2)

maxdec
maxdec

Reputation: 5725

Just like that:

<h3 class="title">Book</h3>

<% book.each do |key, value| %>
  <h4><%= key %></h4>
  <ul>
    <% value.each do |title| %>
      <li><%= title %></li>
    <% end %>
  </ul>
<% end %>

Upvotes: 1

Sahil Dhankhar
Sahil Dhankhar

Reputation: 3656

<h3 class="title">Book</h3>
<% book.each do |author,books| %>
  <h4><%= author%></h4>
    <ul>
     <% books.each do |book| %>
        <li><%= book%></li>
      <% end %>
    </ul>
<% end %>

Upvotes: 1

Related Questions