5
\$\begingroup\$
<% i = 0 %> 
<% @patients.each do |patient| %> 
 <tr class="<%= i % 2 == 0 ? 'Even' : 'Odd' %>">
 <td><%= link_to patient.id, patient %></td>
 <td><%= patient.user.username %></td>
 <td><%= patient.user.first_name %></td>
 <td><%= patient.user.last_name %></td>
 <td><%= patient.user.email %></td>
 <td><%= patient.user.active %></td>
 <td><%= patient.user.disabled %></td>
 <td>
 <ul class="Horizlist">
 <li><%= link_to 'Detail', patient %></li>
 </ul>
 </td>
 </tr>
<% i += 1 %> 

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

200_success
145k22 gold badges190 silver badges478 bronze badges
asked Feb 24, 2011 at 23:18
\$\endgroup\$

2 Answers 2

12
\$\begingroup\$

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.

<% @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 %>

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)
answered Feb 25, 2011 at 5:14
\$\endgroup\$
2
  • \$\begingroup\$ Damn, how did I not know that? This is of course better than defining your own helper. +1 \$\endgroup\$ Commented Feb 25, 2011 at 10:08
  • \$\begingroup\$ Also, if this is just for styling purposes: you can do that with CSS without classes. \$\endgroup\$ Commented Jan 31, 2012 at 19:43
3
\$\begingroup\$

First of all ruby has an each_with_index method, so you can do @patients.each_with_index do |patient, i| instead of keeping a counter manually.

However with the conditional inside the loop that is still too much logic for the view in my opinion. What I'd do is define a helper for this, which might look like this:

# For each item in the given collection, yield that item and embed the
# result in the given tag. The class attribute of that tag alternates
# between class1 and class2
def alternating(collection, tag, class1, class2, html => {})
 collection.each_with_index do |item, i|
 html[:class] = i % 2 == 0 ? class1 : class2
 content_tag(tag, html, false) do
 yield item
 end
 end
end

And then call it like this:

<% alternating(@patients, :tr, 'Even', 'Odd') do |patient| %> 
 <td><%= link_to patient.id, patient %></td>
 <td><%= patient.user.username %></td>
 <td><%= patient.user.first_name %></td>
 <td><%= patient.user.last_name %></td>
 <td><%= patient.user.email %></td>
 <td><%= patient.user.active %></td>
 <td><%= patient.user.disabled %></td>
 <td>
 <ul class="Horizlist">
 <li><%= link_to 'Detail', patient %></li>
 </ul>
 </td>
<% end %>
answered Feb 25, 2011 at 0:06
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.