# Table headings in a partial template--possible? advisable?

**URL:** <https://www.ruby-forum.com/t/table-headings-in-a-partial-template-possible-advisable/132892>\
**Category:** Rails\
**Created:** [March 25, 2008, 5:08pm UTC](https://www.ruby-forum.com/t/table-headings-in-a-partial-template-possible-advisable/132892 "2008-03-25T17:08:57Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![rpardee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e47c2d/32.png) [@rpardee](https://www.ruby-forum.com/u/rpardee)\
**Post date:** [March 25, 2008, 5:08pm UTC](https://www.ruby-forum.com/t/table-headings-in-a-partial-template-possible-advisable/132892/1 "2008-03-25T17:08:57Z")

</div>

Hey All,

I’ve got 2 views that show a tabular listing of my Person objects (one  
for people/index, and one for organizations/show). Here’s what the  
organizations/show bit looks like:

\<% if @organization.people.count \> 0 then %\>

## People from \<%= h @organization.name %\>
 \<%= render(:partial =\> "shared/person", :collection =\> @organization.people) %\> 

| Name | Organization | Phone | E-Mail | Typical Role | Notes |
| --- | --- | --- | --- | --- | --- |

\<% end %\>

As you can see–I’m starting the table & setting the headings in the  
partial-calling template. This meant that when I added an attribute  
to my Person class, I had to edit the 2 templates that called the  
partial. Not tragic or anything, but I’m guessing not optimal either.

So what’s the DRY way to do this? Am I right that by the time we’re  
in the partial template we’re iterating through a collection, and it’s  
too late to do anything pre-amble-y? Maybe a helper?

Also–bonus question: is there a graceful way of suppressing the  
printout of the Organization column when the partial is called from  
organizations/show? It’s really redundant in that case…

Thanks much!

-Roy

---

<div class="post-metadata">

**Author:** ![rpardee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e47c2d/32.png) [@rpardee](https://www.ruby-forum.com/u/rpardee)\
**Post date:** [March 25, 2008, 5:44pm UTC](https://www.ruby-forum.com/t/table-headings-in-a-partial-template-possible-advisable/132892/2 "2008-03-25T17:44:08Z")

</div>

Roy P. wrote:

> ```
> <th>Name</th>
> 
> ```
> 
> \<% end %\>  
> Also–bonus question: is there a graceful way of suppressing the  
> printout of the Organization column when the partial is called from  
> organizations/show? It’s really redundant in that case…
> 
> Thanks much!
> 
> -Roy

Since you appear to be just listing people in both, then both should  
probably be run off the people controller index action. If you setup  
RESTful routes, you’re two URLs would be /people (all people) and  
/organizations/42/people (just the people in org #42). Both urls will  
go to the people\_controller index action. Then in your index action on  
that controller, you can do something like

def index  
conditions = String.new  
conditions += “organization\_id = #{params[:organization\_id]}”  
@people = Person.find(:all, :conditions =\> conditions)  
etc. etc. etc.

If you really do need to have two separate pages with different  
information, and both just happen to also list some people, then yes,  
moving that table off into some shared partial that both can use would  
be much DRYer and recommended. For instance, maybe  
/app/views/shared/\_people\_table.html.erb which from either view you  
could call with \<%= render :partial =\> ‘shared/people\_table’, :locals  
=\> {:people =\> @organization.people } %\>

Have Fun!

–  
[http://www.5valleys.com/](http://www.5valleys.com/)

[http://www.workingwithrails.com/person/8078](http://www.workingwithrails.com/person/8078)

---

<div class="post-metadata">

**Author:** ![rpardee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e47c2d/32.png) [@rpardee](https://www.ruby-forum.com/u/rpardee)\
**Post date:** [March 25, 2008, 7:32pm UTC](https://www.ruby-forum.com/t/table-headings-in-a-partial-template-possible-advisable/132892/3 "2008-03-25T19:32:08Z")

</div>

Hmmm–interesting–thanks for the response!

So do I take it that it’s _not_ true that partials only ever iterate  
through the collection you pass in? You can do preface type stuff,  
like start up a

 and define a header row? I was inferring that  
you couldn’t from the fact that there’s no explicit iteration  
happening in the partial–no “@collection.each do |item|” type stuff.

I suppose I should just test it–I was assuming I’d get a separate  
table for each Person if I put the

 stuff in the  
partial.

---

<div class="post-metadata">

**Author:** ![rpardee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e47c2d/32.png) [@rpardee](https://www.ruby-forum.com/u/rpardee)\
**Post date:** [March 25, 2008, 10:38pm UTC](https://www.ruby-forum.com/t/table-headings-in-a-partial-template-possible-advisable/132892/4 "2008-03-25T22:38:54Z")

</div>

Got it–that makes perfect sense. Thanks! I’m tempted to just do one  
level of partial, so the header row & detail row code are all  
together. I think I’ll start out that way & see if it’s more or less  
painful over time.

So I guess if I want to suppress that ‘organization’ field in the  
table that winds up on organization/show, I can pass in another local  
var holding say, a list of fields to suppress, or a flag or some such  
thing…

Thanks!

-Roy

---

<div class="post-metadata">

**Author:** ![rpardee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e47c2d/32.png) [@rpardee](https://www.ruby-forum.com/u/rpardee)\
**Post date:** [March 25, 2008, 7:38pm UTC](https://www.ruby-forum.com/t/table-headings-in-a-partial-template-possible-advisable/132892/5 "2008-03-25T19:38:36Z")

</div>

Roy P. wrote:

> partial.

True, when you add the “:collection =\> argument” to your “render  
:partial”, then it will iterate over each item in the collection, but  
you don’t have to pass a collection if you only want to render it once.  
So, you could have one partial that sets up the table headers and is  
called from several pages, and it calls another partial that iterates  
over in items in the passed collection.

–  
[http://www.5valleys.com/](http://www.5valleys.com/)

[http://www.workingwithrails.com/person/8078](http://www.workingwithrails.com/person/8078)
