I’m trying to create a zebra-striped series of divs with the following code:
= content_tag_for(:div, conversation.receipts_for(current_user), :class => cycle("odd", "even")) do |receipt|
%p=receipt.content
In theory, the class names should cycle between “receipt odd” and “receipt even” for each row. Instead, I get “receipt odd” every single time. I’ve tried using unordered lists and tables as well, but they don’t work properly either. Any idea what’s going on?
This can’t work, the way you’ve written it.
cycleis called once at the time you callcontent_tag_for, and it returns"odd". It is that value,"odd", that is passed intocontent_tag_for, not the functioncycle. Unlesscontent_tag_foraccepts a block/lambda for itsstyleargument, you cannot do what you are trying to do.In essence, you’re calling a function and passing in the return value of a second function:
The best way to handle this is via collection rendering.
In your view:
In a separate partial, probably
app/views/receipts/_receipt.html.haml: