HTML Data Sheets in Rails 3

I have a model Messagethat has a creation date. How can I do this so that I can display the date horizontally and other attributes vertically.

So, it will 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 / gem plugin that I could use in Rails, or do I need to use some JavaScript library for this?

0
source share
1 answer

prepare data in your controller

@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 the views:

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

All Articles