I have an hash like this:
@json = [{"id"=> 1, "username" => "Example"}, {"id"=> 2, "username" => "Example 2"}]
I want to do something like this:
<ul>
<% @json.each do |user| %>
<li><%= user.username %></li>
<% end %>
</ul>
and it would output a list with the two usernames.
Just tried this in IRB:
json2 = [{"id"=> 1, "username" => "Example"}, {"id"=> 2, "username" => "Example 2"}]
irb(main):076:0> json2.each do |user|
irb(main):077:1* user["id"]
irb(main):078:1> end
=> [{"id"=>1, "username"=>"Example"}, {"id"=>2, "username"=>"Example 2"}]
irb(main):079:0>
What you have there is a
Hash, not aUserobject. Therefore, you must access the username using the index operator ([]):