1

I have a hash with many values. For each of the values I want to generate a list entry.
When I do the following, only the last entry from the hash is displayed.

<% data.each do |k, v| %>
    <li><%= v['name'] %> - <%= v['email'] %></li>
<% end %>

data is loaded from a YAML

person:
   name: ABC
   email: [email protected]
person:
   name: LMN
   email: [email protected]

How do I add all the values from hash in a html list? Thanks

1
  • 1
    The only thing that looks fishy is your variable called Config. This shows up on most lists of Rails' reserved words, so it's probably a good idea to name your variable something else (use lowercase letters, also). Commented Mar 22, 2013 at 0:43

1 Answer 1

1

If that's your actual YAML file, the the reason you are only seeing one object is because your keys are all person. If you parse this into a hash, each instance of person is going to be stored on the person key, and since hash keys must be unique, only the last record is persisted.

You probably want to structure this as an array of hashes (or "sequence of mappings" in YAML speak):

---
- name: LMN
  email: [email protected]
- name: ABC
  email: [email protected]

Then iterate as:

<% data.each do |entry| %>
  <li><%= entry['name'] %> - <%= entry['email'] %></li>
<% end %>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.