“喜欢"、“不喜欢"导轨插件
是否有任何喜欢"、不喜欢"的 Rails 插件...
Is there any "like" , "dislike" plugin for rails...
我浏览了评级插件...但它们都是 5 星级评级插件...
I went through rating plugins... but all of them were 5 star rating plugins...
我建议通过采用经典的投票模型功能来创建 like
和 dislike
选项.
I recommend creating the like
and dislike
option by taking on the classic vote model functionality.
所以你有 Vote
作为 User
和 Votable Item
之间的连接表.
So you have Vote
as a join table between the User
and the Votable Item
.
投票值可以作为投票值 + 1 = 喜欢,投票值 -1 = 不喜欢,投票值 = 中立/未投票.
A Vote value can work as Vote.value + 1 = Like, Vote.value -1 = Dislike, Vote.value = Neutral/Didn't vote.
您的可投票项目的控制器如下所示:
Your controller for your votable item can look like this :
def like
get_vote
@vote.value += 1 unless @vote.value == 1
@vote.save
respond_to do |format|
format.html
format.js
end
end
def dislike
get_vote
@vote.value -= 1 unless @vote.value == -1
@vote.save
respond_to do |format|
format.html
format.js
end
end
private
def get_vote
current_item = @item.detect{|r| r.id == params[:id].to_i}
@vote = current_item.votes.find_by_user_id(current_user.id)
unless @vote
@vote = Vote.create(:user_id => current_user.id, :value => 0)
current_item.votes << @vote
end
end
对于每个人的信息,这个问题不应该被否决.它完全有效.
And for everyone's info, this question didn't deserve to be voted down. Its completely valid.