How to update table rows with javascript in rails partial -
How to update table rows with javascript in rails partial -
i have table:
<table class="articles"> <%= render :partial => 'articles/article' , :collection => @articles %> </table>
and next partial:
<tr> <td> <div id="vote_form"> <% form_tag url_for(article_votes_path(article)), :remote => true %> <%= image_submit_tag("vote-arrow-up.png", :alt => "vote-up", :id => "voteup_button") %> <% end %> </div> <div id="vote_score"> <% form_tag url_for(article_votes_path(article)), :remote => true %> <%= image_submit_tag("vote-arrow-down.png", :alt => "vote-down", :id => "votedown_button") %> <% end %> </div> <div id="vote_score"> votes: <%= article.votes.count %> </div> </td> </tr>
the javascript using update vote score asynchronously is:
$("vote_score").update('<%= "votes: #{@article.votes.count}" %>')
the code works , updates articles' corresponding counts correctly in first table row. instance, if vote on article in 3rd row 5 votes, score updates asynchronously , 6 displayed in first row. , if vote on article in 5th row 100 votes, score in first row display 101. when page reloaded, each article has right vote count. how can modify javascript update count in vote button's respective table row?
element ids supposed unique across document. that's why first element gets updated.
you should assign ids unique. example, utilize article id:
<div id="vote_score_<%= article.id %>">
javascript ruby-on-rails ajax
Comments
Post a Comment