【问题标题】:Update 'like' count in Rails with Ajax使用 Ajax 更新 Rails 中的“喜欢”计数
【发布时间】:2016-05-06 03:18:16
【问题描述】:

我研究了许多关于此主题的其他 SO 帖子,但似乎找不到与我的情况相匹配的帖子。

我的网站有帖子,用户可以点赞,增加点赞数。如果我在没有 Ajax 的情况下执行此操作并且只使用页面刷新,则计数器会正确递增并且按钮会根据需要更改颜色。事实上,我的 Ajax 代码在单击时改变了按钮的颜色,只是点赞数没有增加。

服务器日志似乎也显示请求被处理为JS并持久化到数据库,那么为什么没有刷新计数呢?

likes_controller.rb

def create
    @post = Post.find(params[:post_id])
    @like = @post.likes.create(user_id: current_user.id)
    respond_to do |format|
      format.html {redirect_to :back}
      format.js {}
    end
end

def destroy
    @like = Like.find_by(post_id: params[:post_id], user_id: params[:user_id]).destroy
    respond_to do |format|
      format.html {redirect_to :back}
      format.js {}
    end
end

_like_post.html.erb

<% if current_user.already_likes?(post) %>
    <%= link_to "<i class='fa fa-thumbs-up icon'></i>#{post.likes.count}".html_safe, like_path(post_id: post.id, user_id: current_user.id), method: :delete, class: 'btn btn-default stat-item active', id: "unlike-post", remote: true %>
<% else %>
    <%= link_to "<i class='fa fa-thumbs-up icon'></i>#{post.likes.count}".html_safe, likes_path(post_id: post.id), method: :post, class: 'btn btn-default stat-item', id: "like-post", remote: true %>
<% end %> 

create.js.erb

$("#unlike-post").show();

destroy.js.erb

$("#like-post").show();

编辑:

我最初在解决方案中遇到错误,因为我的 likes 路由没有嵌套在帖子中。我改变了它,还改变了随后的类似和不同路径以反映嵌套变化(分别为post_likes_pathpost_like_path)。我还将我的喜欢控制器中的销毁方法调整为@like = Like.find(params[:id]).destroy。最后,我将@like 作为变量传递给了like 路径,这样整个路径就变成了:post_like_path(@like, post_id: post.id)

【问题讨论】:

    标签: javascript ruby-on-rails ajax


    【解决方案1】:

    当你喜欢或不喜欢create.js.erbdestroy.js.erb的帖子时,你需要重新渲染部分文件_like_post.html.erb

    您有$("#unlike-post").show();,但它不会更新页面中的任何内容; .show() 唯一的作用(等效地)是将元素的 css 更改为 display: block

    说了这么多,下面应该可以正常工作了

    _like_post.html.erb

    <div id='like-post-container'>
      <% if current_user.already_likes?(post) %>
          <%= link_to "<i class='fa fa-thumbs-up icon'></i>#{post.likes.count}".html_safe, like_path(post_id: post.id, user_id: current_user.id), method: :delete, class: 'btn btn-default stat-item active', id: "unlike-post", remote: true %>
      <% else %>
          <%= link_to "<i class='fa fa-thumbs-up icon'></i>#{post.likes.count}".html_safe, likes_path(post_id: post.id), method: :post, class: 'btn btn-default stat-item', id: "like-post", remote: true %>
      <% end %>
    </div>
    

    create.js.erb

    $('#like-post-container').html('<%= j render partial: "likes/like_post", locals: {post: @post} %>');
    

    destroy.js.erb

    $('#like-post-container').html('<%= j render partial: "likes/like_post", locals: {post: @like.post} %>');
    

    请注意,这种方法将重新创建元素。如果您在这些元素上有 JS 绑定,它将被删除,并且可能无法正常工作。如果您有绑定,那么您将不得不直接更新计数值,而不是像我上面的实现那样。

    【讨论】:

    • 我收到 500 错误。 undefined local variable or method "post"。这是因为它是部分的,我需要为 JS 定义“post”是什么?
    • 已更新。现在传递post 变量。
    • 我认为我们非常接近,但现在我遇到了路由问题。服务器说(No route matches {:action=&gt;"destroy", :controller=&gt;"likes", :post_id=&gt;928, :user_id=&gt;1} missing required keys: [:id])。我认为参数传递正确?
    • 我认为该错误与您的routes.rb 有关。您在_like_post.html.erb 上面的第 3 行实际上并没有将 ID 作为参数的一部分传递给它(您只传递了 post_id 和 user_id),所以我认为我的回答不会影响它。您没有使用 RESTful 约定来创建和销毁操作,因此您的路由应该类似于 controller :likes do post 'create'; delete 'destroy'(注意没有传递 id)
    【解决方案2】:

    只需更新您的 js.erb 字段即可更新计数

    create.js.erb

    $("#unlike-post").html("<%= j "<i class='fa fa-thumbs-up icon'></i>#{@post.likes.count}".html_safe %>")
    $("#unlike-post").show();
    

    destroy.js.erb

    $("#like-post").html("<%= j "<i class='fa fa-thumbs-up icon'></i>#{@like.post.likes.count}".html_safe %>")
    $("#like-post").show();
    

    希望对您有所帮助。 :)

    【讨论】:

    • 刚试过这个,我得到了与其他帖子相同的错误(可能与问题无关)。错误是(No route matches {:action=&gt;"destroy", :controller=&gt;"likes", :post_id=&gt;928, :user_id=&gt;1} missing required keys: [:id])
    • 即使在我的编辑中进行了调整之后,我还是无法让这个解决方案发挥作用。
    • 那是另一个问题。所以因为“like_path”需要密钥:id所以我在这种情况下建议2个选项。 1. 您可以将假 id 传递给路径 like_path(id: 1, .....) 因为您的操作中不需要此参数。 2.(推荐)为post找到current_user的like传给路径like_path(the_like_object, .....)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-09
    • 2013-03-11
    • 1970-01-01
    • 1970-01-01
    • 2012-07-24
    • 1970-01-01
    相关资源
    最近更新 更多