Niels
Niels

Reputation: 69

Show Ruby count result as html

I have created a custom method that will return unique items, together with the number of occurrences in a table. I have the following code:

@answers = Answer.all.count(:group => 'answer')

The result is fine when rendering it to JSON, showing:

{"0,0":1,"1,2":1,"2,2":1,"3,3":1}

...but I am unable to render the result to html. I can not display the key, values:

<% @answers.each do |answer| %>
  <tr>
    <td>key: <%= @answer.??? %></td>
    <td>value: <%= @answer.??? %></td>
  </tr>
<% end %>

Upvotes: 0

Views: 71

Answers (1)

user1284966
user1284966

Reputation: 51

If answers is a hash (and it looks like it is) you can pass the key and value into the rendering block by using two parameters instead of one.

like this:

<% @answers.each do |key, val| %>
  <tr>
    <td>key: <%= key %></td>
    <td>value: <%= val %></td>
  </tr>
<% end %>

Upvotes: 1

Related Questions