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?
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>
精彩评论