使用ajax点击投票按钮的Rails autorefresh div
我希望在有人点击投票按钮后自动刷新div计算总票数。 投票已经登记,但不会令人耳目一新。
我做过这样的事情;
和vote_up.js.erb看起来像这样;
getElementById().html("");
而vote_down.js.erb看起来像这样
getElementById().html("");
控制器动作看起来像这样;
def vote_up @school_review = SchoolReview.find(params[:id]) @school_review.liked_by current_user respond_to do |format| #format.html {redirect_to school_reviews_path} format.js { } end end def vote_down @school_review = SchoolReview.find(params[:id]) @school_review.downvote_from current_user respond_to do |format| #format.html {redirect_to school_reviews_path} format.js { } end end
在控制台中,我明白了
GET http://0.0.0.0:3000/vote_up [HTTP/1.1 200 OK 290ms] GET http://0.0.0.0:3000/vote_up [HTTP/1.1 304 Not Modified 86ms]
谢谢
JQuery的
你在JQuery和纯JS之间感到困惑
–
你对getElementByID
的调用是一个javascript调用 – 需要附加到document
对象,如Octopus-Paul
,这是你的第一个问题
第二个问题是你在相应的objects
上调用html
。 .html
是一个JQuery函数 ; javascript等价于.innerHTML
你会做得更好:
#app/views/controller/action.html.erb $("<%=j 'up_votes_count-#{@school_review.id}' %>").html("<%=j @school_review.get_likes.size %>");
这使用纯JQuery,它允许您将相应的数据附加到页面