Javascript rails4ajax调用只工作一次

Javascript rails4ajax调用只工作一次,javascript,jquery,ruby-on-rails,ajax,Javascript,Jquery,Ruby On Rails,Ajax,我有一个应用程序,您可以在其中发布链接。每个链接都有许多注释 我已经设置了ajax,用户可以在其中上传特定的评论。目前,用户第一次可以通过ajax成功地向上投票一条评论,但是如果用户随后尝试在同一页面上向上投票一条不同的评论,那么ajax将中断,并且在刷新页面之前不会更新该值 评论\u controller.rb: def comment_upvote @comment = Comment.find(params[:id]) @link = Link.where(id: @comment

我有一个应用程序,您可以在其中发布
链接
。每个
链接
都有许多
注释

我已经设置了ajax,用户可以在其中上传特定的评论。目前,用户第一次可以通过ajax成功地向上投票一条评论,但是如果用户随后尝试在同一页面上向上投票一条不同的评论,那么ajax将中断,并且在刷新页面之前不会更新该值

评论\u controller.rb:

def comment_upvote
  @comment = Comment.find(params[:id])
  @link = Link.where(id: @comment.link_id)
  @comment.upvote_by current_user
  respond_to do |format|
    format.html {redirect_to link_path(@link)}
    format.js {}
  end
end  
<div class="well">
  <h2><%= comment.title %></h2>
  <p class="text-muted">Added by <strong><%= comment.author %> <%= comment.author_last_name %></strong> on
  <%= l(comment.created_at, format: '%B, %d %Y %H:%M:%S') %></p>
  <blockquote>
    <p><%= comment.body %></p>
  </blockquote>
  <p><%= link_to 'reply', new_comment_path(parent_id: comment.id, link_id: @link.id)  %></p>

  <%= link_to pointup_comment_path(comment.id), method: :put, remote: true do %>
    +
  <% end %>
  <div id="comment-votes">
    Votes: <%= comment.get_upvotes.size %>
  </div>
$('#comment-votes').html("<%= j render "upvotes", locals: { @comment => @comment } %>")
Votes: <%= @comment.get_upvotes.size %>
<div id="comment-votes-<%= comment.id %>">
  Votes: <%= comment.get_upvotes.size %>
</div>
 $("#comment-votes-<%= @comment.id %>").html("<%= j render "upvotes", locals: { @comment => @comment } %>")
views/comments/\u comment.html.erb:

def comment_upvote
  @comment = Comment.find(params[:id])
  @link = Link.where(id: @comment.link_id)
  @comment.upvote_by current_user
  respond_to do |format|
    format.html {redirect_to link_path(@link)}
    format.js {}
  end
end  
<div class="well">
  <h2><%= comment.title %></h2>
  <p class="text-muted">Added by <strong><%= comment.author %> <%= comment.author_last_name %></strong> on
  <%= l(comment.created_at, format: '%B, %d %Y %H:%M:%S') %></p>
  <blockquote>
    <p><%= comment.body %></p>
  </blockquote>
  <p><%= link_to 'reply', new_comment_path(parent_id: comment.id, link_id: @link.id)  %></p>

  <%= link_to pointup_comment_path(comment.id), method: :put, remote: true do %>
    +
  <% end %>
  <div id="comment-votes">
    Votes: <%= comment.get_upvotes.size %>
  </div>
$('#comment-votes').html("<%= j render "upvotes", locals: { @comment => @comment } %>")
Votes: <%= @comment.get_upvotes.size %>
<div id="comment-votes-<%= comment.id %>">
  Votes: <%= comment.get_upvotes.size %>
</div>
 $("#comment-votes-<%= @comment.id %>").html("<%= j render "upvotes", locals: { @comment => @comment } %>")

+ 投票:

views/comments/comment\u upvote.js.erb:

def comment_upvote
  @comment = Comment.find(params[:id])
  @link = Link.where(id: @comment.link_id)
  @comment.upvote_by current_user
  respond_to do |format|
    format.html {redirect_to link_path(@link)}
    format.js {}
  end
end  
<div class="well">
  <h2><%= comment.title %></h2>
  <p class="text-muted">Added by <strong><%= comment.author %> <%= comment.author_last_name %></strong> on
  <%= l(comment.created_at, format: '%B, %d %Y %H:%M:%S') %></p>
  <blockquote>
    <p><%= comment.body %></p>
  </blockquote>
  <p><%= link_to 'reply', new_comment_path(parent_id: comment.id, link_id: @link.id)  %></p>

  <%= link_to pointup_comment_path(comment.id), method: :put, remote: true do %>
    +
  <% end %>
  <div id="comment-votes">
    Votes: <%= comment.get_upvotes.size %>
  </div>
$('#comment-votes').html("<%= j render "upvotes", locals: { @comment => @comment } %>")
Votes: <%= @comment.get_upvotes.size %>
<div id="comment-votes-<%= comment.id %>">
  Votes: <%= comment.get_upvotes.size %>
</div>
 $("#comment-votes-<%= @comment.id %>").html("<%= j render "upvotes", locals: { @comment => @comment } %>")
$(“#评论投票”).html(“@comment}%>”)
views/comments/\u upvows.html.erb:

def comment_upvote
  @comment = Comment.find(params[:id])
  @link = Link.where(id: @comment.link_id)
  @comment.upvote_by current_user
  respond_to do |format|
    format.html {redirect_to link_path(@link)}
    format.js {}
  end
end  
<div class="well">
  <h2><%= comment.title %></h2>
  <p class="text-muted">Added by <strong><%= comment.author %> <%= comment.author_last_name %></strong> on
  <%= l(comment.created_at, format: '%B, %d %Y %H:%M:%S') %></p>
  <blockquote>
    <p><%= comment.body %></p>
  </blockquote>
  <p><%= link_to 'reply', new_comment_path(parent_id: comment.id, link_id: @link.id)  %></p>

  <%= link_to pointup_comment_path(comment.id), method: :put, remote: true do %>
    +
  <% end %>
  <div id="comment-votes">
    Votes: <%= comment.get_upvotes.size %>
  </div>
$('#comment-votes').html("<%= j render "upvotes", locals: { @comment => @comment } %>")
Votes: <%= @comment.get_upvotes.size %>
<div id="comment-votes-<%= comment.id %>">
  Votes: <%= comment.get_upvotes.size %>
</div>
 $("#comment-votes-<%= @comment.id %>").html("<%= j render "upvotes", locals: { @comment => @comment } %>")
投票:

有没有简单的方法来解决这个问题?如果您需要更多详细信息,请告诉我。

这里的问题是,有许多div的id
注释投票
。当您试图通过
id
获取元素时,它总是得到相同的div

要解决此问题,需要使每个注释的
id
唯一

views/comments/\u comment.html.erb:

def comment_upvote
  @comment = Comment.find(params[:id])
  @link = Link.where(id: @comment.link_id)
  @comment.upvote_by current_user
  respond_to do |format|
    format.html {redirect_to link_path(@link)}
    format.js {}
  end
end  
<div class="well">
  <h2><%= comment.title %></h2>
  <p class="text-muted">Added by <strong><%= comment.author %> <%= comment.author_last_name %></strong> on
  <%= l(comment.created_at, format: '%B, %d %Y %H:%M:%S') %></p>
  <blockquote>
    <p><%= comment.body %></p>
  </blockquote>
  <p><%= link_to 'reply', new_comment_path(parent_id: comment.id, link_id: @link.id)  %></p>

  <%= link_to pointup_comment_path(comment.id), method: :put, remote: true do %>
    +
  <% end %>
  <div id="comment-votes">
    Votes: <%= comment.get_upvotes.size %>
  </div>
$('#comment-votes').html("<%= j render "upvotes", locals: { @comment => @comment } %>")
Votes: <%= @comment.get_upvotes.size %>
<div id="comment-votes-<%= comment.id %>">
  Votes: <%= comment.get_upvotes.size %>
</div>
 $("#comment-votes-<%= @comment.id %>").html("<%= j render "upvotes", locals: { @comment => @comment } %>")

投票:
设置唯一注释ID后。您只需要更改ajax调用

views/comments/comment\u upvote.js.erb:

def comment_upvote
  @comment = Comment.find(params[:id])
  @link = Link.where(id: @comment.link_id)
  @comment.upvote_by current_user
  respond_to do |format|
    format.html {redirect_to link_path(@link)}
    format.js {}
  end
end  
<div class="well">
  <h2><%= comment.title %></h2>
  <p class="text-muted">Added by <strong><%= comment.author %> <%= comment.author_last_name %></strong> on
  <%= l(comment.created_at, format: '%B, %d %Y %H:%M:%S') %></p>
  <blockquote>
    <p><%= comment.body %></p>
  </blockquote>
  <p><%= link_to 'reply', new_comment_path(parent_id: comment.id, link_id: @link.id)  %></p>

  <%= link_to pointup_comment_path(comment.id), method: :put, remote: true do %>
    +
  <% end %>
  <div id="comment-votes">
    Votes: <%= comment.get_upvotes.size %>
  </div>
$('#comment-votes').html("<%= j render "upvotes", locals: { @comment => @comment } %>")
Votes: <%= @comment.get_upvotes.size %>
<div id="comment-votes-<%= comment.id %>">
  Votes: <%= comment.get_upvotes.size %>
</div>
 $("#comment-votes-<%= @comment.id %>").html("<%= j render "upvotes", locals: { @comment => @comment } %>")
$(“#评论投票-”).html(“@comment}%>”)

这里的问题是,有许多div的id
是注释投票
。当您试图通过
id
获取元素时,它总是得到相同的div

要解决此问题,需要使每个注释的
id
唯一

views/comments/\u comment.html.erb:

def comment_upvote
  @comment = Comment.find(params[:id])
  @link = Link.where(id: @comment.link_id)
  @comment.upvote_by current_user
  respond_to do |format|
    format.html {redirect_to link_path(@link)}
    format.js {}
  end
end  
<div class="well">
  <h2><%= comment.title %></h2>
  <p class="text-muted">Added by <strong><%= comment.author %> <%= comment.author_last_name %></strong> on
  <%= l(comment.created_at, format: '%B, %d %Y %H:%M:%S') %></p>
  <blockquote>
    <p><%= comment.body %></p>
  </blockquote>
  <p><%= link_to 'reply', new_comment_path(parent_id: comment.id, link_id: @link.id)  %></p>

  <%= link_to pointup_comment_path(comment.id), method: :put, remote: true do %>
    +
  <% end %>
  <div id="comment-votes">
    Votes: <%= comment.get_upvotes.size %>
  </div>
$('#comment-votes').html("<%= j render "upvotes", locals: { @comment => @comment } %>")
Votes: <%= @comment.get_upvotes.size %>
<div id="comment-votes-<%= comment.id %>">
  Votes: <%= comment.get_upvotes.size %>
</div>
 $("#comment-votes-<%= @comment.id %>").html("<%= j render "upvotes", locals: { @comment => @comment } %>")

投票:
设置唯一注释ID后。您只需要更改ajax调用

views/comments/comment\u upvote.js.erb:

def comment_upvote
  @comment = Comment.find(params[:id])
  @link = Link.where(id: @comment.link_id)
  @comment.upvote_by current_user
  respond_to do |format|
    format.html {redirect_to link_path(@link)}
    format.js {}
  end
end  
<div class="well">
  <h2><%= comment.title %></h2>
  <p class="text-muted">Added by <strong><%= comment.author %> <%= comment.author_last_name %></strong> on
  <%= l(comment.created_at, format: '%B, %d %Y %H:%M:%S') %></p>
  <blockquote>
    <p><%= comment.body %></p>
  </blockquote>
  <p><%= link_to 'reply', new_comment_path(parent_id: comment.id, link_id: @link.id)  %></p>

  <%= link_to pointup_comment_path(comment.id), method: :put, remote: true do %>
    +
  <% end %>
  <div id="comment-votes">
    Votes: <%= comment.get_upvotes.size %>
  </div>
$('#comment-votes').html("<%= j render "upvotes", locals: { @comment => @comment } %>")
Votes: <%= @comment.get_upvotes.size %>
<div id="comment-votes-<%= comment.id %>">
  Votes: <%= comment.get_upvotes.size %>
</div>
 $("#comment-votes-<%= @comment.id %>").html("<%= j render "upvotes", locals: { @comment => @comment } %>")
$(“#评论投票-”).html(“@comment}%>”)

为什么要获取一个数组中的所有UpVote,然后返回数组的大小?为什么不运行sql计数查询?我问的是
@注释。get_upvotes.size
我正在使用一个名为acts_as_votable的gem进行投票,这就是问题所在。为什么要在一个数组中获取所有upvotes,然后返回数组的大小?为什么不运行sql计数查询?我在问关于
@的评论。get_upvoces.size
我正在使用一个名为acts_as_votable的gem进行投票,这只是它的api