Topic: Rails Loops and Styling Debacle

<h1>Listing contacts</h1>

<table>
  <tr>
  <% for column in Contact.content_columns %>
    <th><%= column.human_name %></th>
  <% end %>
  </tr>
  
<% for contact in @contacts %>
  <tr>
  <% for column in Contact.content_columns %>
    <td><%=h contact.send(column.name) %></td>
  <% end %>
    <td><%= link_to 'Show', :controller => 'users', :action => 'show', :id => contact.user.id %></td>
    <td><%= link_to 'Destroy', { :action => 'destroy', :id => contact }, :confirm => 'Are you sure?', :method => :post %></td>
  </tr>
<% end %>
</table>

The above is really nice code, but I'm fit to be tied on how to style it. As it is now its pretty bad. I'm afraid I may have to break the loops to do it right. Not really looking forward to that (I don't think the programmer would appreciate it either). I have code and styles for nice lists but they are more HTML and less dynamic.

Any ideas on approaching this one?

"Bear 270, young man. Bear two, seven, zero, over." - Musings of a flight simulator guru, me.

Re: Rails Loops and Styling Debacle

Yea, first off that code is terrible, even though it does what you need. There's no thead, tfoot or tbody for one. There is no reason to be using content_columns and there is no reason to be running the loop three times.

So, here is a much better way of doing this:

<table cellspacing="0" cellpadding="0">
		<col class=""></col>
		<col class=""></col>
	
	<thead>
		<tr>
			<th>Column</th>
			<th>Column</th>
		</tr>
	</thead>
	
	<tfoot></tfoot>
	
	
	<tbody>
		<% @contacts.each do |contact| %>
			<tr>
				<td><%= h contact.name %></td>
				<td><%= link_to 'Show', :controller => 'users', :action => 'show', :id => contact.user.id %></td>
		    <td><%= link_to 'Destroy', { :action => 'destroy', :id => contact }, :confirm => 'Are you sure?', :method => :post %></td>
			</tr>
		<% end %>
	</tbody>

</table>

Re: Rails Loops and Styling Debacle

Thanks. I meant that I liked the code in that it was short, but yours is short too. I'm giving it a shot today.

"Bear 270, young man. Bear two, seven, zero, over." - Musings of a flight simulator guru, me.

Re: Rails Loops and Styling Debacle

This should give you everything you need to style the table, including all your column headings automatically:

<table id="contacts" border="0" cellspacing="0" cellpadding="0">
  <thead>
    <tr>
      <% for column in Contact.content_columns %>
        <%= content_tag(:th, h(contact.send(column.name))) %>
      <% end %>
      
      <!-- spans show/destroy -->
      <td colspan="2">Actions</td>
    </tr>
  </thead>
  
  <tbody>
    <% for contact in @contacts %>
      <tr>
        <% for column in Contact.content_columns %>
          <%= content_tag(:th, contact.send(column.name)) %>
        <% end %>
        
        <!-- actions -->
        <td><%= link_to 'Show', :controller => 'users', :action => 'show', :id => contact.user.id %></td>
        <td><%= link_to 'Destroy', { :action => 'destroy', :id => contact }, :confirm => 'Are you sure?', :method => :post %></td>
      </tr>
    <% end %>
  </tbody>
</table>

Here's a nice article on styling tables: http://veerle.duoh.com/blog/comments/a_ … version_2/

Ryan Heneise  |  Art of Mission  |  Now with extra-strong Donor Tools mojo

Re: Rails Loops and Styling Debacle

Leovenous wrote:

Thanks. I meant that I liked the code in that it was short, but yours is short too. I'm giving it a shot today.

Sorry if I came off short - been on a lot of pain killers due multiple kidney stones.

One thing that I don't that rubyists do a lot is substitute ruby for html or javascript. (No offense to Ryan or his post above) In my opinion, it is unnecessary processing that could easily be written in the language it should be written in.  (if you look at the content_tag source, you'll call that method, which calls several other methods, etc. No code is faster than code, everytime, hence my preference to use just html). That's what I meant [unnecessary use of ruby] by my original sharp comment.