Skip to content

Instantly share code, notes, and snippets.

@bloudermilk
Created September 27, 2012 20:01
Show Gist options
  • Save bloudermilk/3796152 to your computer and use it in GitHub Desktop.
Save bloudermilk/3796152 to your computer and use it in GitHub Desktop.
A syntax for tables
module Admin::TicketsHelper
def admin_tickets_table(tickets)
Tabular.build(tickets) do |table|
# If we're simply adding a few columns where the header would be the
# humanized attribute name and the row would contain the attribute `.to_s`
# then we can use the simple `#columns` method.
table.columns :id, :title
# If the column has custom logic for row values, we can use the `#column`
# method to define it
table.column :milestone do |ticket, milestone|
milestone ? milestone.name : "Backlog"
end
# Tables frequently have action links in the rightmost column. Here's how
# we could achieve that. Since we're in a helper we have all the Rails
# view helpers available to us.
table.column "Actions" do |ticket|
link_to "Edit", edit_admin_ticket_path(ticket)
end
end
end
end
<!-- Extremely simple tables are supported out of the box -->
<%= simple_table_for @tickets, columns: %w[number title state], actions: %w[edit destroy] %>
<!-- Use custom tables -->
<%= admin_tickets_table(@tickets) %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment