简体   繁体   English

Ajax成功更新投票计数器

[英]vote counter updating with Ajax success

I am creating a voting system with Ajax and the vote counter would show the result dynamically. 我正在使用Ajax创建投票系统,投票计数器将动态显示结果。 I can create the vote and delete the vote with Ajax successfully. 我可以创建投票并成功使用Ajax删除投票。 However, I could not update the vote counter dynamically. 但是,我无法动态更新投票计数器。 I did a lot of trial and error and running out of options. 我做了很多次尝试和错误,没有其他选择。

<div id="counter"><%= @micropost.thumbups.count %></div> , in a div with id, counter, shows vote numbers correctly on microposts/show page. <div id="counter"><%= @micropost.thumbups.count %></div>在ID为counter的div中,在microposts / show页面上正确显示投票号。

On create.js.erb, 在create.js.erb上,

$("#ups_form").html("<%= escape_javascript(render('microposts/down_form')) %>")

$("#counter").html('<%= @micropost.thumbups.count %>')

The second line is supposed to make the counter to update count dynamically after Ajax has successfully updated the database. 第二行应该使计数器在Ajax成功更新数据库之后动态更新计数。 But it did not work. 但这没有用。

I tried with the following: 我尝试了以下方法:

  1. $("#counter").html('hello') , "hello" would show. $("#counter").html('hello') ,将显示“ hello”。 this means CSS is fine. 这意味着CSS很好。
  2. $("#counter").html('<%=escape_javascript @micropost.thumbups.count %>') did not work. $("#counter").html('<%=escape_javascript @micropost.thumbups.count %>')无效。
  3. $("#counter").html("<%= escape_javascript(render('microposts/count')) %>") , after making a count partial of <%= @micropost.thumbups.count %> $("#counter").html("<%= escape_javascript(render('microposts/count')) %>") ,然后将部分计数计为<%= @micropost.thumbups.count %>
  4. ...don't know what else to try... ...不知道还有什么尝试...

Try this code: 试试这个代码:

$("#counter").html('');
$("#counter").html('<%= j @micropost.thumbups.count %>');

Your association is probably already loaded in to memory. 您的关联可能已加载到内存中。 Try this: 尝试这个:

$("#counter").html('<%= @micropost.thumbups(:reload).count %>')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM