Rails 3 中的 HTML 数据表 [英] HTML data tables in Rails 3

查看:29
本文介绍了Rails 3 中的 HTML 数据表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Message 模型,它有一个创建日期.我怎样才能让它水平显示日期,垂直显示其他属性.

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.

所以它会是这样的:

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

是否有我可以在 Rails 中使用的插件/gem,或者我必须使用一些 JavaScript 库来做到这一点?

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 }

然后在视图中:

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

这篇关于Rails 3 中的 HTML 数据表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆