None
Usage
The Integer#even? method is helpful when adding zebra striping to views.
For example:
<% @customers.each_with_index do |customer, index| %>
<div style="color:<%= index.even? ? "white" : "gray" %>">
<%= customer.name %>
</div>
<% end %>
Test
unit_tests do
test "10 is even?" do
assert_equal true, 10.even?
end
end
The above usage assumes you have rows that are never deleted. To ensure that the "zebra striping" always occurs one should look into the cycle method provided by rails:
ReplyDeletehttp://api.rubyonrails.org/classes/ActionView/Helpers/TextHelper.html#M000634
The above comment is not entirely accurate. "Each with index" refers to the array @customers, that is populated from the controller. "Index" refers to the "index" in the array, not the ActiveRecord ID from the database.
ReplyDeleteI agree, though, on the point about cycle. Cycle is much more powerful inside an iteration of a loop because it accepts a variable amount of values:
<div style="color:<%= cycle("white", "gray", "red")" %> >