ed1t
ed1t

Reputation: 8689

HTML data tables in Rails 3

I have a Message model which has a create date. How can I make it so that I can display the date horizontally and the other attributes vertically.

So it would be something like this:

Date         1/1/11           2/1/11            3/1/11
Message      message1         message2          message3
Attr 1       attr1 val        ....              ...
Attr 2       attr2 val        ....              ......

Is there a plugin/gem that I could use in Rails, or I have to use some JavaScript library to do that?

Upvotes: 0

Views: 452

Answers (1)

fl00r
fl00r

Reputation: 83680

in your controller prepare data

@messages = Message.select(:created_at, :message, :attr1, :attr2 ...)
@turned_messages = @messages.all.inject({}){ |h, c| c.attributes.each{ |k,v| h[k] ||= []; h[k] << v }; h }

Then in views:

<table>
  <% @turned_messages.each do |k, values| %>
    <tr>
      <td><%= k %></td>
      <% values.each do |v| %>
        <td><%= v %></td>
      <% end %>
    </tr>
  <% end %>
</table>

Upvotes: 4

Related Questions