HiveBrain v1.2.0
Get Started
← Back to all entries
patternrubyrailsModerate

Rails view to list users in a zebra-striped table

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
viewrailszebrastripedlistuserstable

Problem

 
 
 ">
    
    
    
    
    
    
    
    
      
        
      
    
  


I don't like that I have to define an i variable, is there a better way?

Solution

Rails has a built in method, cycle, which will cycle between two more more options for you, so you don't have to manage the "Even"/"Odd" selection.


   cycle('Even', 'Odd') do %>
    
    
      
    
    
      
        
      
    
  


If you happen to use Haml, it looks quite nice. :)

- @patients.each do |patient|
  %tr{:class => cycle('Even', 'Odd')}
    %td= link_to(patient.id, patient)
    - [:username, :first_name, :last_name, :email, :active, :disabled].each do |property|
      %td= patient.user.send(property)
    %td
      %ul.Horizlist
        %li= link_to('Detail', patient)

Code Snippets

<% @patients.each do |patient| %>
  <%= content_tag :tr, :class => cycle('Even', 'Odd') do %>
    <%= content_tag :td, link_to(patient.id, patient) %>
    <% [:username, :first_name, :last_name, :email, :active, :disabled].each do |property| %>
      <%= content_tag :td, patient.user.send(property) %>
    <% end %>
    <td>
      <ul class="Horizlist">
        <%= content_tag :li, link_to('Detail', patient) %>
      </ul>
    </td>
  <% end %>
<% end %>
- @patients.each do |patient|
  %tr{:class => cycle('Even', 'Odd')}
    %td= link_to(patient.id, patient)
    - [:username, :first_name, :last_name, :email, :active, :disabled].each do |property|
      %td= patient.user.send(property)
    %td
      %ul.Horizlist
        %li= link_to('Detail', patient)

Context

StackExchange Code Review Q#978, answer score: 12

Revisions (0)

No revisions yet.