1

When I send a request to a function I get back an array with a nested hashes looking like this (i formatted it a bit for readability):

@variable = [{:"Some ID"=>1, :"Some area"=>"Berlin", :"Company Name"=>"the dummy company", :"A Date"=>2014-01-17 02:15:53 +0100, :"First Name"=>"Michael", :"Last Name"=>"Myers", :"Number Average"=>#<BigDecimal:7fcf546f3da0,'0.5E1',9(18)>, :"Number One"=>5, :"Number Two"=>5, :Comment=>"Tippi Toppi", :Count=>2},
{:"Some ID"=>2, :"Some area"=>"Berlin", :"Company Name"=>"the dummy company", :"A Date"=>2014-01-17 02:15:54 +0100, :"First Name"=>"Michael", :"Last Name"=>"Myers", :"Number Average"=>#<BigDecimal:7fcf546f3bc0,'0.5E1',9(18)>, :"Number One"=>5, :"Number Two"=>5, :Comment=>"OHA", :Count=>2}]

What would I like to do? I want to display the values of the hashes in a table like:

ID | Some Area | Company Name | ...

1 | Berlin | The Dummy company | ...

2 | Berlin | The Dummy company | ...

I have found this answer and tried it, but I get a undefined method 'data' for #<Hash: ... >.

View

- headers = @variable.map(&:data).flat_map(&:keys).uniq #<< error points here

      %tr
        - headers.each do |key|
          %th= key

      - @variable.each do |tr|
        %tr
          - header.each do |key|
            %td= tr.data[key]

3 Answers 3

1

From the thread you referenced data is an attribute and is not a method on the standard Ruby Hash. Other than that your code seemed fine.

- headers = @variable.flat_map(&:keys).uniq #<< error used to occur here

  %tr
    - headers.each do |key|
      %th= key

  - @variable.each do |tr|
    %tr
      - headers.each do |key|
        %td= tr[key]

Aside: It is good practice to keep logic out of the views, so I'd recommend moving this code into a controller or model headers = @variable.flat_map(&:keys).uniq

Sign up to request clarification or add additional context in comments.

1 Comment

This works like a charm! Thanks! As for moving the logic - will do so - thanks for the reminder :)
0

If I see this right your main question would be how to collect the hash keys into a header array. (I'm assuming all hashes in @variable have an identical structure)

@headers = @variables.first.keys

should be all you need here.

@rows = @variables.map(&:values)

should give you the rows with data only.

2 Comments

Hey Thorsten - it now says "undefined method keys for nil:NilClass". If I leave the "first" and just add "keys" it says "undefined method keys for array[]". Any idea?
@timo Your array was most likely empty. Or the first element of the array was nil.
0

A simple way would be

<% @variable.each do |variable| %>
  <%= variable["some id"] | variable["some area"]... %>
<% end %>

Edit: To make it dynamic, you can try something like

<% @variable.each do |variable| %>
  <% variable.each do |key,value| %>
    <%= key : value %>
  <% end %>
<% end %>

1 Comment

But then it is not dynamic anymore. I would like it to be dynamic so that it changes in case I want to change the array/hash

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.