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 %>
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?
<%=
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.
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?
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
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?
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:
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!
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.
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.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.