ruby - conditional formatting in rails partials -
i rendering rails partial , want alternate background color when renders partial. know not super clear here example of want do:
row 1 grey background row 2 yellow background row 3 grey background row 4 yellow background- sorry stackoverflow seams prevent background colors being shown think makes idea clear
this view code using
<table> <%= render :partial => 'row' :collection => @rows %> </table>
the _row.html.erb partial looks this
<tr bgcolor="#aaaaaa"> <td><%= row.name %></td> </tr>
the problem not know how change background color every other row. there way this?
you use cycle helper. this:
<tr class="<%= cycle("even", "odd") %>"> <td><%= row.name %></td> </tr>
or in case use bgcolor instead, although recomend using css classes.
you can cycle through more 2 values: cycle(‘first’, ‘second’, ‘third’, ‘and_more’).
there also: reset_cycle(‘cycle_name’) makes sure on each iteration, start again first value of cycle list.
check rails documentation more examples.
Comments
Post a Comment