这段代码很好。但 每次我需要重新加载页面,看看像按钮一样。不知道为什么没有回应。 show.html.erb
<span class="like-count"><%= pluralize(@property.cached_votes_up, 'like') %></span>
<%= render 'shared/actions', resource: @property %>
共享/ _actions.html.erb
<% if user_signed_in? %>
<%= render 'likes/form', likeable: resource %>
<% end %>
喜欢/ _form.html.erb
<% if current_user.liked? likeable %>
<%= form_tag unlike_path(likeable_type: likeable.class.to_s, likeable_id: likeable.id), method: :post, remote: true, data: { type: :json } do %>
<% button_tag class: 'btn btn-block liked' do %>
<%= fa_icon 'thumbs-up' %> unlike
<% end %>
<% end %>
<% else %>
<%= form_tag like_path(likeable_type: likeable.class.to_s, likeable_id: likeable.id), remote: true, data: { type: :json } do %>
<% button_tag class: 'btn btn-block' do %>
<%= fa_icon 'thumbs-up' %> like
<% end %>
<% end %>
<% end %>
喜欢/ create.js.erb
$("#<%= @likeable_type.downcase %>-<%= @likeable.id %> .like").html("<%=j render('likes/form', likeable: @likeable) %>");
$("#<%= @likeable_type.downcase %>-<%= @likeable.id %> .like-count").html("<%= pluralize(@likeable.cached_votes_up, 'like') %>");
喜欢/ destroy.js.erb
$("#<%= @likeable_type.downcase %>-<%= @likeable.id %> .like").html("<%=j render('likes/form', likeable: @likeable) %>");
$("#<%= @likeable_type.downcase %>-<%= @likeable.id %> .like-count").html("<%= pluralize(@likeable.cached_votes_up, 'like') %>");
likes_controller.rb
class LikesController < ApplicationController
before_action :find_likeable
respond_to :js
def create
@likeable.liked_by current_user
@likeable.create_activity(:like, owner: current_user)
respond_to do |format|
format.js
end
end
def destroy
@likeable.disliked_by current_user
activity = PublicActivity::Activity.find_by_trackable_id_and_key(@likeable.id, "#{@likeable_type.downcase}.like")
activity.destroy if activity.present?
respond_to do |format|
format.js
end
end
private
def find_likeable
@likeable_type = params[:likeable_type].classify
@likeable = @likeable_type.constantize.find(params[:likeable_id])
end
end
likes_helper.rb
module LikesHelper
def find_like(likeable)
likeable.get_likes.where(user: current_user)
end
end
不知道为什么它不起作用。任何想法?
答案 0 :(得分:0)
试试这个。它会切换相似的按钮。
<%= form_tag unlike_path(likeable_type: likeable.class.to_s, likeable_id: likeable.id), method: :post, remote: true, data: { type: :json }, class: "unlike #{"hidden" unless current_user.liked?(likeable)}" do %>
<% button_tag class: 'btn btn-block liked' do %>
<%= fa_icon 'thumbs-up' %> unlike
<% end %>
<% end %>
<%= form_tag like_path(likeable_type: likeable.class.to_s, likeable_id: likeable.id), remote: true, data: { type: :json }, class: "unlike #{"hidden" if current_user.liked?(likeable)}" do %>
<% button_tag class: 'btn btn-block' do %>
<%= fa_icon 'thumbs-up' %> like
<% end %>
<% end %>
<script type="text/javascript">
$(function() {
$(".unlike").on("ajax:success", function() {
$(".unlike").addClass("hidden");
$(".like").removeClass("hidden");
});
$(".like").on("ajax:success", function() {
$(".like").addClass("hidden");
$(".unlike").removeClass("hidden");
});
})
</script>
<style type="text/css">
.hidden {
display: none;
}
</style>
您可能希望使用ajax:beforeSend
添加加载邮件。