RJS - replacing html for multiple entries

Hi,

El Gato wrote:

I may have, for example, 3 posts that have been repeated on a page, all with the same id (e.g, <div id="post-11">) and I want to perform a replace_html against all of them. Doing it the normal way just replaces the first. Can someone help? Thanks!

What you have is invalid HTML / XMTML. DOM ID's must be unique within a page for either Ajax or CSS to work properly (and don't even get me started on browser behavior in quirks mode :wink: ). Run your page source against http://validator.w3.org/. Assuming you really need to put the same post on the page multiple times, you'll need to come up with a new naming approach. Something like sec1-post-11, sec2-post-11, etc. isn't tough to either render or process on return.

hth, Bill

Hi,

El Gato wrote:

Actually, I knew that, and was originally using classes because of it, but due to the documentation, thought I'd test id with ids to see if it would work. So, is there no way to replace_html against all elements of a class?

replace_html replaces the content of a single DOM element, specified by id. To replace the contents of multiple DOM elements, you'd just iterate. That's very easy to do in your rjs template since it allows a mix of straight ruby and rjs.

The problem with just using a unique naming scheme is that I want *all* of the elements of that class affected by the replace... I can't think of a clean way to interrogate the page about all of these "unique" ids and change each one then.

Not sure what you mean by 'interrogate the page'. It sounds like maybe you're coming from a background that's mostly client-side js. In RoR, every thing that's on the page, you put there from the server side where it's very easy to keep track of. So I typically don't think about interrogating the page, I think about interrogating the database or the session store to find out what's on the page. In the example you've given so far, my impression is you've got one 'thing' that appears in different sections of the page. One (not tested, and it's early here :wink: ) approach to replacing all those elements could look something like...

sections = ['sec1-', 'sec2-', 'sec3-] sections.each do {|this_section|   element_to_replace = this_section+@post_to_replace   page.replace_html(element_to_replace, ... }

I'm assuming the @post_to_replace was identified in the controller method. The sections could, of course, be identified there too.

hth, Bill