chrishomer chrishomer - 10 months ago 108
Ruby Question

Ruby on Rails HTML-Table Generator

I am looking for a good RoR table generator (or an easy solution) that can give me a decent view of my records in a table (unstylized but proper strict XHTML).

Let's say I have a User model and an Address model:
- A User can have many Addresses
- One address is also linked as the "primary_address"

Let's say I have the following in my User controller

def index
@users = User.find(:all,:order => 'id ASC')
@headers = ["id","First","Last","City","State"]
@fields = [:id,:firstname,:lastname,,:primary_address.state]

I don't know if the array of fields would work but I think it gets the point across. Does anyone know a good gem, plugin or technique for this so that I don't have to "repeat myself" on all my table views?

Answer Source

@ChrisH: Representing table using two arrays won't give more control. I would suggest the following: table_helper

erb snippet -

collection_table(@posts, {}, :id => 'posts', :class => 'summary') do |header, body|
  header.column :title
  header.column :category
  header.column :author
  header.column :publish_date, 'Date< br \>Published'
  header.column :num_comments, '# Comments'
  header.column :num_trackbacks, '# Trackbacks'

  body.alternate = true do |row, post, index|
    row.publish_date   time_ago_in_words(post.published_on)
    row.num_comments   post.comments.empty? ? '-' : post.comments.size
    row.num_trackbacks post.trackbacks.empty? ? '-' : post.trackbacks.size