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 %> <p><span class=''commentdate''><%comment.created_at.strftime("#{comment.created_at.day.ordinalize} %b %Y") %></span> <strong><%= h(comment.name) %></strong><span class=''grey''> says:</span></p> <p><%= h(comment.content) %></p> <% 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. -- Posted via http://www.ruby-forum.com/.
On Wed, Sep 30, 2009 at 1:30 PM, Shaun Pearl <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > 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 %> > <p><span class=''commentdate''><%> comment.created_at.strftime("#{comment.created_at.day.ordinalize} %b > %Y") %></span> > <strong><%= h(comment.name) %></strong><span class=''grey''> > says:</span></p> > <p><%= h(comment.content) %></p> > <% 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.I often use javascript to set the class names of the rows after the page renders. Here are many examples: http://blog.jquery.com/2006/10/18/zebra-table-showdown/ -- Greg Donald http://destiney.com/
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? -- Posted via http://www.ruby-forum.com/.
On Wed, Sep 30, 2009 at 11:45, Shaun Pearl <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> 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.
On Wed, Sep 30, 2009 at 1:45 PM, Shaun Pearl <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> 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 Donald http://destiney.com/
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 :) -- Posted via http://www.ruby-forum.com/.
On Wed, Sep 30, 2009 at 2:05 PM, Shaun Pearl <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> 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 Donald http://destiney.com/
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! -- Posted via http://www.ruby-forum.com/.
2009/9/30 Shaun Pearl <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>:> > 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