# Cycle/Partials issue

**URL:** <https://www.ruby-forum.com/t/cycle-partials-issue/176456>\
**Category:** Rails\
**Created:** [September 30, 2009, 8:30pm UTC](https://www.ruby-forum.com/t/cycle-partials-issue/176456 "2009-09-30T20:30:26Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![shaunp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/35a633/32.png) [@shaunp](https://www.ruby-forum.com/u/shaunp)\
**Post date:** [September 30, 2009, 8:30pm UTC](https://www.ruby-forum.com/t/cycle-partials-issue/176456/1 "2009-09-30T20:30:26Z")

</div>

First off, I’m fairly new to ruby on rails so this may appear simple but  
I’m just learning the ropes 🙂

So I have a simple blog app where a post has many comments. The ‘show’  
view for posts renders a partial for each comment the post has. The css  
class of the containing div element is cycled using cycle(). The partial  
code is,

\<% div\_for(comment, :class =\> cycle(“blue”,“white”)) do %\>

\<%= comment.created\_at.strftime("#{comment.created\_at.day.ordinalize} %b %Y") %\>**\<%= h(comment.name) %\>** says:

\<%= h(comment.content) %\>

\<% end %\>

This works fine when you first render the page… however when you add a  
new comment via ajax it inserts an additional partial… but the cycle  
seems to have reset itself and the css class will always be blue. Is  
there a means to make sure that the cycle continues on from the previous  
rendering of the partial? Or perhaps a more elegant way of doing it  
altogether?

Thanks.

---

<div class="post-metadata">

**Author:** ![shaunp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/35a633/32.png) [@shaunp](https://www.ruby-forum.com/u/shaunp)\
**Post date:** [September 30, 2009, 8:38pm UTC](https://www.ruby-forum.com/t/cycle-partials-issue/176456/2 "2009-09-30T20:38:40Z")

</div>

On Wed, Sep 30, 2009 at 1:30 PM, Shaun P.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> \<%= seems to have reset itself and the css class will always be blue. Is there a means to make sure that the cycle continues on from the previous rendering of the partial? Or perhaps a more elegant way of doing it altogether?
> 
> Thanks.

I often use javascript to set the class names of the rows after the  
page renders.

Here are many examples:

> **[Zebra Table Showdown | Official jQuery Blog](http://blog.jquery.com/2006/10/18/zebra-table-showdown/)**
>
> jQuery: The Write Less, Do More, JavaScript Library

–  
Greg D.  
[http://destiney.com/](http://destiney.com/)

---

<div class="post-metadata">

**Author:** ![shaunp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/35a633/32.png) [@shaunp](https://www.ruby-forum.com/u/shaunp)\
**Post date:** [September 30, 2009, 8:45pm UTC](https://www.ruby-forum.com/t/cycle-partials-issue/176456/3 "2009-09-30T20:45:57Z")

</div>

Thanks 🙂 The link is greatly appreciated! But I was hoping for a  
solution from within rails really, mainly for my learning purposes, I  
apprecaite doing it that way isn’t really going to cause any harm, but  
there most be a solution to the problem without resorting to javascript?

---

<div class="post-metadata">

**Author:** ![shaunp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/35a633/32.png) [@shaunp](https://www.ruby-forum.com/u/shaunp)\
**Post date:** [September 30, 2009, 9:05pm UTC](https://www.ruby-forum.com/t/cycle-partials-issue/176456/4 "2009-09-30T21:05:55Z")

</div>

I guess my real question is,

Why does the cycle reset on subsequent renderings of the partial? Doing  
it with JS is fine, I’m more curious as to why this doesn’t work though.  
Apologies if my intent wasn’t too clear in the original post!

I shall just do it using JS for now anyway, but would love to know whats  
going on behind the scenes in the example I gave 🙂

---

<div class="post-metadata">

**Author:** ![shaunp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/35a633/32.png) [@shaunp](https://www.ruby-forum.com/u/shaunp)\
**Post date:** [September 30, 2009, 8:56pm UTC](https://www.ruby-forum.com/t/cycle-partials-issue/176456/5 "2009-09-30T20:56:15Z")

</div>

On Wed, Sep 30, 2009 at 1:45 PM, Shaun P.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Thanks 🙂 The link is greatly appreciated! But I was hoping for a  
> solution from within rails really,

Are you aware Rails _comes_ with Prototype and Scriptaculous as  
built-in javascript libraries? Any Rails helper you find that has  
“remote” in the name uses javascript.

> mainly for my learning purposes, I  
> apprecaite doing it that way isn’t really going to cause any harm, but  
> there most be a solution to the problem without resorting to javascript?

It’s 2009, it’s ok to use javascript… seriously.

–  
Greg D.  
[http://destiney.com/](http://destiney.com/)

---

<div class="post-metadata">

**Author:** ![shaunp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/35a633/32.png) [@shaunp](https://www.ruby-forum.com/u/shaunp)\
**Post date:** [September 30, 2009, 9:21pm UTC](https://www.ruby-forum.com/t/cycle-partials-issue/176456/6 "2009-09-30T21:21:25Z")

</div>

On Wed, Sep 30, 2009 at 2:05 PM, Shaun P.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I guess my real question is,
> 
> Why does the cycle reset on subsequent renderings of the partial? Doing  
> it with JS is fine, I’m more curious as to why this doesn’t work though.  
> Apologies if my intent wasn’t too clear in the original post!
> 
> I shall just do it using JS for now anyway, but would love to know whats  
> going on behind the scenes in the example I gave 🙂

If you want it to know what style/class to draw next you need a better  
cycle() method, one that tracks it’s own state across multiple  
requests. I have this alt method in one of my Rails apps:

def alt( s=‘’, s2=’ class=“alt-row”’ )  
session[:alt] ||= ‘1’  
session[:alt] = session[:alt] == ‘1’ ? ‘2’ : ‘1’  
session[:alt] == ‘1’ ? s2 : s  
end

Usage is just: \<tr\<%= alt %\>

But using jQuery is just as easy:

$(“tr:nth-child(odd)”).addClass(“odd”);

–  
Greg D.  
[http://destiney.com/](http://destiney.com/)

---

<div class="post-metadata">

**Author:** ![shaunp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/35a633/32.png) [@shaunp](https://www.ruby-forum.com/u/shaunp)\
**Post date:** [September 30, 2009, 9:42pm UTC](https://www.ruby-forum.com/t/cycle-partials-issue/176456/7 "2009-09-30T21:42:50Z")

</div>

Nice thanks!

I’m guessing different http requests cause a new cycle object to be  
instantiated meaning it will be back to the inital style? I couldn’t  
find an answer within the API.

Anyway that’s great I shall implement my own cycle as per your advice,  
cheers!

---

<div class="post-metadata">

**Author:** ![shaunp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/35a633/32.png) [@shaunp](https://www.ruby-forum.com/u/shaunp)\
**Post date:** [September 30, 2009, 10:01pm UTC](https://www.ruby-forum.com/t/cycle-partials-issue/176456/8 "2009-09-30T22:01:49Z")

</div>

2009/9/30 Shaun P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Nice thanks!
> 
> I’m guessing different http requests cause a new cycle object to be  
> instantiated meaning it will be back to the inital style? I couldn’t  
> find an answer within the API.

Remember that cycle() is executed in the server as a result of your  
ajax call to it. It has no way of knowing what state the cycle was at  
from the last call. In the extreme it might not even be the same  
server as last time (though probably this is not the case here). You  
could save the state in the session as suggested by another post or  
you could pass the current state as a parameter to the ajax call, or  
probably better something that indicates the row number from which the  
required class can be determined.

Colin

---

<div class="post-metadata">

**Author:** ![shaunp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/35a633/32.png) [@shaunp](https://www.ruby-forum.com/u/shaunp)\
**Post date:** [October 1, 2009, 9:51am UTC](https://www.ruby-forum.com/t/cycle-partials-issue/176456/9 "2009-10-01T09:51:54Z")

</div>

On Wed, Sep 30, 2009 at 11:45, Shaun P.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Thanks 🙂 The link is greatly appreciated! But I was hoping for a  
> solution from within rails really, mainly for my learning purposes, I  
> apprecaite doing it that way isn’t really going to cause any harm, but  
> there most be a solution to the problem without resorting to javascript?

How is it “resorting to JavaScript”, when you’re already doing AJAX  
(AKA: JavaScript)?

If you were using the link that Greg posted to handle doing the  
alternation on non AJAXy pages, I can definitely see why you’d want to  
avoid that (I would, too). On an AJAXy page, it seems, to me, like  
there’s no problem with this solution.
