Mike Caputo
Mike Caputo

Reputation: 1156

How do I create a dynamic class for a div variable?

I have the following code in an ERB file:

<table border="1">
    <% @lists.each do |list| %>
        <tr class="even">
            <td><%= link_to list.title, list %></td>
            <td><%= link_to 'Edit', edit_list_path(list) %></td>
            <td><%= button_to "Destroy", list_path(list), :method => :delete %></td>
        </tr>
    <% end %>
</table>

I want to make the <tr class="even" line dynamic. Each tr should get the class either "even" or "odd" depending on a counter variable that gets incremented every time my loop starts over. However, I cannot figure out the best way to implement this.

Upvotes: 1

Views: 1173

Answers (1)

Mikhail Nikalyukin
Mikhail Nikalyukin

Reputation: 11967

Use this nice helper :)

<tr class="<%= cycle("even", "odd") %>">

Upvotes: 4

Related Questions