Forum: Ruby on Rails cycle() not cycling in a partial

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
D6434aa0b7b350f8c3ed0119d81b2ead?d=identicon&s=25 Roy Pardee (rpardee)
on 2009-01-24 02:01
(Received via mailing list)
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 <tr> tag, it *does* work.

  <!-- class="<%= cycle('line-even', 'line-odd') %>" -->

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

  <% this_found_project_id = "fp_#{found_project.id}" %>
  <tr class="<%= cycle('line-even', 'line-odd')%>" id="<%=
this_found_project_id %>">
    <td><%= found_project.name %></td>
    <td>
      <%=
        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
      %>
    </td>
  </tr>

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

Thanks!

-Roy

Roy Pardee
Research Analyst/Programmer
Group Health Center For Health Studies (Cancer Research Network)
(206) 287-2078
Google Talk: rpardee
Aafa8848c4b764f080b1b31a51eab73d?d=identicon&s=25 Phlip (Guest)
on 2009-01-24 16:42
(Received via mailing list)
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 <tr> tag, it *does* work.
>
>   <!-- class="<%= cycle('line-even', 'line-odd') %>" -->

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}" %>
>   <tr class="<%= cycle('line-even', 'line-odd')%>" id="<%= this_found_project_id %>">
>     <td><%= found_project.name %></td>
>     <td>
>       <%=
>         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 <a> tag's onclick
attribute. And
this happens for every <tr> 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>
>   </tr>

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
D6434aa0b7b350f8c3ed0119d81b2ead?d=identicon&s=25 Roy Pardee (rpardee)
on 2009-01-26 17:13
(Received via mailing list)
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
This topic is locked and can not be replied to.