我可以点击链接一次,计数会更新,但之后的点击不会更新,也不会更新到数据库中(查看控制台)。
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
的routes.rb
resources :skits do
resources :challenges
member do
get "like", to: "skits#like"
get "unlike", to: "skits#unlike"
end
end
unlike.js.erb
$('.unlike-post').bind('ajax:success', function(){
$(this).find('.vote-count').html('<%= escape_javascript @skit.votes_for.size.to_s %>');
});
like.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
修改
从底部应用帮助后,不保存到数据库中:
保存到数据库时:
答案 0 :(得分:1)
要添加到@max
的评论,您需要查看DRY代码(相应地使用HTTP Verbs
和actions
):
# 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
将在请求结束时运行代码。虽然这可能需要调整,但它应该可以工作。
答案 1 :(得分:1)
这里的问题是您为此请求使用的HTTP方法,当您想从服务器获取信息时使用GET,但如果您想更新资源,则GET不是正确的方法。
如果我是你,我会使用PATCH方法:
HTTP方法PATCH可用于更新部分资源。对于 例如,当您只需要更新资源的一个字段时, 提供完整的资源表示可能很麻烦 利用更多带宽
resources :skits do
resources :challenges
member do
patch :like
patch :unlike
end
end
更新haml中的method
:
.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
将为您处理