# Cycle() not cycling in a partial

**URL:** <https://www.ruby-forum.com/t/cycle-not-cycling-in-a-partial/158509>\
**Category:** Rails\
**Created:** [January 24, 2009, 2:01am UTC](https://www.ruby-forum.com/t/cycle-not-cycling-in-a-partial/158509 "2009-01-24T02:01:44Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rpardee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e47c2d/32.png) [@rpardee](https://www.ruby-forum.com/u/rpardee)\
**Post date:** [January 24, 2009, 2:01am UTC](https://www.ruby-forum.com/t/cycle-not-cycling-in-a-partial/158509/1 "2009-01-24T02:01:44Z")

</div>

Hey All,

I’ve got the below partial rendering each item of a collection to a  
table row, with some nice ajax magic for hiding the row if the user  
clicks a link. It works very nicely, except for the bit that assigns  
the class= attribute to the tr tag. For some reason I can’t figure out,  
every row is getting class=“line-even”. Does anybody have an idea why  
the cycle() call wouldn’t work here?

For extra weirdness, if I include this html comment line right before  
the line w/the opening

 tag, it _does_ work. 

############### non-cycling partial ########################

\<% this\_found\_project\_id = “fp\_#{found\_project.id}” %\>

 \<%= found\_project.name %\> \<%= link\_to\_function 'add' do |page| page.insert\_html :bottom, "roster", :partial =\> 'tumor\_sites/tumor\_sites\_project', :object =\> TumorSitesProject.new(:project\_id =\> found\_project.id) page.hide this\_found\_project\_id end %\> 

############### non-cycling partial ########################

Thanks!

-Roy

Roy P.  
Research Analyst/Programmer  
Group Health Center For Health Studies (Cancer Research Network)  
(206) 287-2078  
Google Talk: rpardee

---

<div class="post-metadata">

**Author:** ![rpardee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e47c2d/32.png) [@rpardee](https://www.ruby-forum.com/u/rpardee)\
**Post date:** [January 24, 2009, 4:42pm UTC](https://www.ruby-forum.com/t/cycle-not-cycling-in-a-partial/158509/2 "2009-01-24T16:42:30Z")

</div>

Pardee, Roy wrote:

> I’ve got the below partial rendering each item of a collection to a table row, with some nice ajax magic for hiding the row if the user clicks a link. It works very nicely, except for the bit that assigns the class= attribute to the tr tag. For some reason I can’t figure out, every row is getting class=“line-even”. Does anybody have an idea why the cycle() call wouldn’t work here?

You have written a real tongue-twister here; two minor misunderstandings  
that  
lead to the cycle() canceling itself out!

> For extra weirdness, if I include this html comment line right before the line w/the opening
> 
> tag, it _does_ work. 
> 
> eRB cannot comprehend \<!-- (because you could eRB any other language  
> with its  
> own comments). Hence, the cycle() runs, and pushes one of those classes  
> into a  
> comment! If you viewed the source, or wrote puts @response.body in your  
> unit  
> test, you would see them.
> 
> That increment’s cycle’s ticker by one.
> 
> > ############### non-cycling partial ########################
> > 
> > \<% this\_found\_project\_id = “fp\_#{found\_project.id}” %\>
> > 
> > \<%= found\_project.name %\> \<%= link\_to\_function 'add' do |page|
> > 
> > This is not Ajax. The code calculates this partial once, at page render  
> > time. If  
> > that partial calls cycle(), it will increment its tick again. That might  
> > cancel  
> > out the last cycle().
> > 
> > Then, all this extra HTML gets shoved into this tag’s onclick  
> > attribute. And  
> > this happens for every in this table. This is just normal  
> > JavaScript, not Ajax.
> > 
> > > ```
> > > page.insert_html :bottom, 
> > > "roster", 
> > > :partial => 'tumor_sites/tumor_sites_project', 
> > > :object => TumorSitesProject.new(:project_id => found_project.id)
> > > page.hide this_found_project_id
> > > end
> > > %>
> > > </td>
> > > 
> > > ```
> > 
> >  
> > 
> > Comment the comment out with \<%# cycle … %\>. Then use  
> > link\_to\_remote(), not  
> > link\_to\_function(). It’s only Ajax if the render :update is inside a  
> > real  
> > controller action.
> > 
> >  
> > 
> > –  
> > Phlip

---

<div class="post-metadata">

**Author:** ![rpardee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e47c2d/32.png) [@rpardee](https://www.ruby-forum.com/u/rpardee)\
**Post date:** [January 26, 2009, 5:13pm UTC](https://www.ruby-forum.com/t/cycle-not-cycling-in-a-partial/158509/3 "2009-01-26T17:13:35Z")

</div>

Dude, you have blown my mind. 😉 That explanation makes perfect  
sense–the partial I call in link\_to\_function there does indeed have an  
identical call to cycle (for coloring rows of another table).

I haven’t tried link\_to\_remote, but giving cycle a :name parameter to  
distinguish it from the other cycle call seems to do the trick–e.g.

cycle(‘line-even’, ‘line-odd’, :name =\> ‘found-rows’)

Many thanks!

-Roy
