AJAX to update micropost vote up/down partial only refreshes the most recent post

萝らか妹 提交于 2019-12-09 07:33:26

The reason is that you have multiple div#global in your code (because of the each loop), so the browser will only recognize the first one. Change global from an id to a class and then it should work. I would also add a data-num attribute to the div and assign it the id of the database object (not to be confused with the HTML id). Something like this:

<div class="pull-right badge global" data-num="<%= mp.id %>"><%= render :partial => 'microposts/rating', :locals => {:micro => mp} %></div>

Then, in your increment.js.erb (I think you meant that instead of .js.html...), change it to:

$('div.global[data-num="<%= @micropost.id %>"]').html("<%= escape_javascript render :partial => 'microposts/rating', :locals => {:micro => @micropost} %>");

That way, the JavaScript will always know which one to update because it's based on the database ID.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!