table_for helper

A Rails helper for generating tables from resources collections that works like form_for helper.

Installation

Add it to your Gemfile:

gem 'table_for'

Run the following command to install it:

$ bundle

Or install it yourself as:

$ gem install table_for

Usage

<%= table_for RESOURCES [, TABLE_TAG_OPTIONS] [BLOCK] %>

In the BLOCK you can configurate columns and footer cells

<%= table_for @users do |t|
  t.column :NAME [, TH_TAG_OPTIONS] [VALUE_BUILDER_BLOCK]
  ...
  t.footer :NAME [, TD_TAG_OPTIONS] [VALUE_BUILDER_BLOCK]
  ...
%>

A VALUE_BUILDER_BLOCK must return a value for a cell.

For example, if you place this into a view:

<%= table_for @users, :class => 'table' do |t|
  t.column :email
  t.column Role, :class => 'span3' do |row|
    row.role.title
  end
  t.column :created_at
  t.column :view_action, '' do |row|
    link_to 'view', user_path(row.id)
  end
end %>

It's will render something like this:

<table class="table">
  <thead>
    <tr>
      <th>Email</th>
      <th class="span3">Role</th>
      <th>Created at</th>
      <th></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>user@mail</td>
      <td>Seller</td>
      <td>12.10.12, 14:30</td>
      <td><a href="/users/1">view</a></td>
    </tr>
    ...
  </tbody>
</table>

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Added some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request