Javascript投票作为投票表

Javascript投票作为投票表,javascript,ruby-on-rails,Javascript,Ruby On Rails,我有一个rails应用程序,其中我的Posts模型有注释,并且注释是可votable的。我用acts_作为表 我目前正在对评论进行投票。现在我正在尝试实现一些javascript,这样页面就不必在每次有人对评论投票时都刷新,这样投票就可以通过了 以下是我以前的经历(这是有效的): 在我的评论中: def upvote_post_comment @post = Post.find(params[:post_id]) @comment = @post.comments.find(para

我有一个rails应用程序,其中我的Posts模型有注释,并且注释是可votable的。我用acts_作为表

我目前正在对评论进行投票。现在我正在尝试实现一些javascript,这样页面就不必在每次有人对评论投票时都刷新,这样投票就可以通过了

以下是我以前的经历(这是有效的):

在我的评论中:

  def upvote_post_comment
  @post = Post.find(params[:post_id])
  @comment = @post.comments.find(params[:id])
  @comment.liked_by current_user

  respond_to do |format|
    format.html {redirect_to :back}
  end
end
 def upvote_post_comment
 @post = Post.find(params[:post_id])
 @comment = @post.comments.find(params[:id])
 @comment.liked_by current_user

respond_to do |format|
format.html {redirect_to :back }
format.json { render json: { count: @comment.liked_count } }
end
end
在我看来:

<% if user_signed_in? && current_user != comment.user && !(current_user.voted_for? comment) %>


<%= link_to image_tag(‘vote.png'), like_post_comment_path(@post, comment), method: :put %> <a>
<%= "#{comment.votes.size}"%></a>


<% elsif user_signed_in? && (current_user = comment.user) %>

<%= image_tag(‘voted.png')%><a><%= "#{comment.votes.size}"%></a>

<% else %>

<%= image_tag(‘voted.png')%><a><%= "#{comment.votes.size}"%></a>
<% end %>
在我看来:

<% if user_signed_in? && current_user != comment.user && !(current_user.voted_for? comment) %>


    <%= link_to image_tag(‘vote.png'), like_post_comment_path(@post, comment), method: :put, class: 'vote', remote: true %> 
<a><%= "#{comment.votes.size}"%></a>


  <script>

    $('.vote')
  .on('ajax:send', function () { $(this).addClass('loading'); })
  .on('ajax:complete', function () { $(this).removeClass('loading'); })
  .on('ajax:error', function () { $(this).after('<div class="error">There was an issue.</div>'); })
  .on('ajax:success', function (data) { $(this).html(data.count); });

    </script>


  <% elsif user_signed_in? && (current_user = comment.user) %>

  <%= image_tag(‘voted.png')%><a><%= "#{comment.votes.size}"%></a>


  <% else %>

  <%= image_tag(‘voted.png')%><a><%= "#{comment.votes.size}"%></a>


    <% end %>

如何通过Javascript使投票生效?投票通过后,投票计数更新,投票图标更新为vote.png而不是vote.png?

您的日志显示请求格式为JS

Processing by CommentsController#upvote_post_comment as JS
data:{type::json}
添加到
link_to
方法,以请求类似这样的json格式

<%= link_to image_tag('vote.png'), like_post_comment_path(@post, comment), method: :put, class: 'vote', remote: true, data: { type: :json } %> 
更新JS以使用

.on('ajax:success', function(e, data, status, xhr) { $(this).html(data.count); });

将“ajax:error”函数更改为
.on('ajax:error',函数(e,xhr,status,error){console.log(status);console.log(error);})
并检查浏览器控制台以查看打印出的内容。该注释似乎不存在方法
liked_count
。尝试将
respond_to
json块更改为
format.json{render json:{count:@comment.likes.size}
。将
ajax:success
更改为this
。on('ajax:success',function(e,data,status,xhr){$(this.html(data.count);})。希望这是最后一次修复:)太好了。我很想多帮忙,但现在是格林尼治标准时间凌晨2点15分,我明天还有工作。明天我将尝试回答更多的问题。如果
a
标记位于单击的链接旁边,您可以执行
$(this.next().html(data.count)
,或者您可以给
a
标记一个css类或id并执行
$('.item class').html(data.count)或
$('#item id').html(data.count)
format.json { render json: { count: @comment.likes.size } }
.on('ajax:success', function(e, data, status, xhr) { $(this).html(data.count); });