I'm working on a Ruby on Rails web app, in which I've got several lists of posts. Each post will use Ajax to load their comments. In order of populating them in the right place, I'm doing the following:
Each post has a div with an id formatted as follows: <div id="my_div_<%= post.id %>"></div>
. So, for example if the post.id is 12, the id will be "my_div_12".
My controller's action looks like this:
render :update do |page|
page.replace_html 'my_div_' + params[:post_id].to_s, :partial => 'my_comments_section_partial_path'
end
That works fine only if I have a post only once at the page. But in this site, each post might be listed more than once, because there are several lists (latest posts, popular, tops, etc). And all of them will have their Comments section to show.
The issue now is that, as the comments section functionality is inside a partial view, it will work the same for every type of list (as it's expected), and therefore, it doesn't make the difference between the divs (because they will have the same post.id, and thus, the same div's id).
My question now is: how could I solv this problem? Is there a better and different way for doing this?
Thanks in advance!
EDIT:
Just to clarify what I want, in few words:
I would like the code page.replace_html 'my_div_' + params[:post_id].to_s, :partial => 'my_comments_section_partial_path'
to replace that partial in EVERY div called 'my_div' + params[:post_id].to_s
(because there may be more than one div in the same page with the same id).