我正在开发一个应用程序,该应用程序在许多不同的区域显示表格数据,并且我发现自己经常反复使用相同的HTML表结构。例如,一个特定的表看起来像这样:在Rails 3.1中,如何创建一个使用块样式格式的HTML表生成器
%table.zebra-striped#user-table{ :cellspacing => "0" }
%colgroup
%col{:id => "email"}
%col{:id => "username"}
%col{:id => "sign-in-count"}
%col{:id => "last-sign-in-at"}
%thead
%tr
%th{:id => "email-head", :scope => "col"} E-mail
%th{:id => "username-head", :scope => "col"} Username
%th{:id => "sign-in-count-head", :scope => "col"} Sign Ins
%th{:id => "last-sign-in-at-head", :scope => "col"} Last Sign In
%tbody
- @users.each do |user|
%tr{ :class => zebra }
%td
=h user.email
%td
=h user.username
%td
=h user.sign_in_count
%td
=h user.last_sign_in_at
理想情况下,我想创造某种辅助方法,在那里我可以这样做:
= custom_table_for @users do
= column :email
= column :username do |user|
= link_to user.username, user_path(user)
= column "Sign Ins", :sign_in_count
= column :last_sign_in_at
这样我可以改变的格式列中的数据和列标题名称(如果我对默认值不满意),但会为我生成表格。
我想我可以创建一个正常的帮手,但我不得不使用数组,我不知道如何包括自定义数据格式每列。
active_admin有类似这样的,你可以在这里看到的东西:http://activeadmin.info/docs/3-index-pages/index-as-table.html
任何线索或想法,将不胜感激。
真棒,迫不及待地让这个旋转。我想我可以将模块粘贴在/lib/table_helper.rb文件中,然后我必须在我的应用程序视图中明确包含TableHelper模块以使用它。 – jklina
嗯,很好的问题,我想你可以将它包含在你的应用程序帮助器中? – Gazler
当我回家时,我会玩弄它。我现在肯定有足够的领先优势。 – jklina