我可以单击一次链接,计数会更新,但之后点击不会更新,也不会更新到数据库中(查看控制台)。
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
编辑
从底部应用帮助后,不保存到数据库中:
当它保存到数据库中时: