2

我可以单击一次链接,计数会更新,但之后点击不会更新,也不会更新到数据库中(查看控制台)。

index.html.haml

.votes
  - if current_user.liked? skit
    = link_to unlike_skit_path(skit), method: :get, remote: true, class: "unlike-post" do
      .upvote
        %span.upvote-link
          %i.fa.fa-caret-up
        .vote-count= skit.votes_for.size
  - else
    = link_to like_skit_path(skit), method: :get, remote: true, class: "like-post" do
      .upvote
        %span.upvote-link
          %i.fa.fa-caret-up
        .vote-count= skit.votes_for.size

路线.rb

resources :skits do
  resources :challenges
  member do 
    get "like", to: "skits#like"
    get "unlike", to: "skits#unlike"
  end
end

不像.js.erb

$('.unlike-post').bind('ajax:success', function(){
  $(this).find('.vote-count').html('<%= escape_javascript @skit.votes_for.size.to_s %>');
});

像.js.erb

$('.like-post').bind('ajax:success', function(){
  $(this).find('.vote-count').html('<%= escape_javascript @skit.votes_for.size.to_s %>');
});

skits_controller.rb

def like
  @skit.liked_by current_user
  respond_to do |format|
    format.html { redirect_to skits_path, notice: 'Successfully voted!' }
    format.js { render layout: false }
  end
end

def unlike
  @skit.unliked_by current_user
  respond_to do |format|
    format.html { redirect_to skits_path, notice: 'Successfully voted!' }
    format.js { render layout: false }
  end
end

编辑

从底部应用帮助后,不保存到数据库中:

在此处输入图像描述

当它保存到数据库中时:

在此处输入图像描述

4

2 回答 2

1

这里的问题是您用于此请求的 HTTP 方法,当您想从服务器获取信息时使用 GET,但如果您想更新资源 GET 不是正确的方法。

如果我是你,我会为此使用 PATCH 方法:

HTTP 方法 PATCH 可用于更新部分资源。例如,当您只需要更新资源的一个字段时,PUT 完整的资源表示可能会很麻烦,并且会占用更多带宽

resources :skits do
  resources :challenges
  member do 
    patch :like
    patch :unlike
  end
end

更新method你的haml:

.votes
  - if current_user.liked? skit
    = link_to unlike_skit_path(skit), method: :patch, remote: true, class: "unlike-post" do
      .upvote
        %span.upvote-link
          %i.fa.fa-caret-up
        .vote-count= skit.votes_for.size
  - else
    = link_to like_skit_path(skit), method: :patch, remote: true, class: "like-post" do
      .upvote
        %span.upvote-link
          %i.fa.fa-caret-up
        .vote-count= skit.votes_for.size

还要确保您正在更新您的正确元素,js.erb对此的解决方案是使用@skit.id

#app/views/skits/like.js.erb
$("#<%=j @skit.id %>").find(".vote-count").html('<%=j @skit.votes_for.size.to_s %>');

顺便说一句,您不需要使用ajax:success. format.js会为你处理

于 2016-01-27T14:05:34.840 回答
1

要添加到@max的评论,您需要查看DRY代码(相应地使用您的HTTP Verbsactions):

# config/routes.rb
resources :skits do
  resources :challenges
  match :like, via: [:post, :delete], on: :member #-> url.com/skits/:id/like
end

# app/controllers/skits_controller.rb
class SkitsController < ApplicationController
   def like
       @skit.liked_by current_user   if request.post?
       @skit.unliked_by current_user if request.delete?
       respond_to do |format|
          format.html { redirect_to skits_path, notice: 'Successfully voted!' }
          format.js #-> format.js doesn't render layout anyway
       end       
   end
end

您将能够使用以下内容:

#app/views/skits/index.html.erb
<%= render @skits %>

#app/views/skits/_skit.html.erb
.votes
   - method =  current_user.liked?(skit) ? :post : :delete
   = link_to skit_like_path(skit), method: method, remote: true, id: skit.id do
      .upvote
        %span.upvote-link
          %i.fa.fa-caret-up
        .vote-count= skit.votes_for.size

解决方案

关于更新问题,您需要确保通过以下方式更新正确的元素js.erb

#app/views/skits/like.js.erb
$("#<%=j @skit.id %>").find(".vote-count").html('<%=j @skit.votes_for.size.to_s %>');

不需要将上述代码绑定到ajax:success-- 调用format.js将在请求结束时运行代码。尽管这可能需要调整,但它应该可以工作。

于 2016-01-27T13:57:00.137 回答