views:

1057

answers:

3

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.city,:primary_address.state]
end

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?

+2  A: 

you could make one using a helper?

def table_generator(collection, header_names, fields)
  return false unless collection.any?
  content_tag(:table, :class => "generic-table") do
    content_tag(:thead) do
      content_tag(:tr) do
        header_names.each do |name|
          content_tag(:td, name)
        end
      end
    end
    content_tag(:tbody) do
      collection.each do |col|
        content_tag(:tr) do
          field_names.each do |name|
            content_tag(:td, col.send(name))
          end
        end
      end
    end
  end
end

Use with caution! Untested.

Omar Qureshi
This was a little helpful but it doesn't quite work. Not sure what's going on. Maybe it's my inputs but I cant get it to generate the table. Note that you are also missing end tags after the two td lines.
ChrisH
Sorry, yes, it should have end tags. What parameters are you passing into it?Will try it later on when i have a moment and see if i can debug
Omar Qureshi
A: 

I know this isn't pretty but I was having so many problems with the "content_tag" I decided it wasn't worth my time and just saved in a string. I would much rather use that function but time is more valuable than elegance right now. Maybe I'll go back and figure it out in the future, but for now, this is functional and forces better CSS practices anyway.

def table_generator(collection, header_names, fields, class_name)
    return false unless collection.any?
    table_str = ""
table_str += "<table id=\"" + class_name + "\" class=\"" + class_name + "\">\n"
  table_str += "\t<thead>\n"
    table_str += "\t\t<tr>\n"
      header_names.each do |name|
        table_str += "\t\t\t<th>"
        table_str += name
        table_str += "</th>\n"
      end
    table_str += "\t\t</tr>\n"
  table_str += "\t</thead>\n"
  table_str += "\t<tbody>\n"
    collection.each do |col|
      table_str += "\t\t<tr>\n"
        fields.each do |name|
          table_str += "\t\t\t<td>\n"
            table_str += col[name].to_s
          table_str += "\t\t\t</td>\n"
        end
      table_str += "\t\t</tr>\n"
    end
  table_str += "\t</tbody>\n"
table_str += "</table>\n"
end
ChrisH
+1  A: 

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

http://www.railslodge.com/plugins/767-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
  body.build do |row, post, index|
    row.category       post.category.name
    row.author         post.author.name
    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
  end
end
Ninad Pachpute