“喜欢"、“不喜欢"导轨插件 [英] "Like", "Dislike" plugin for rails

查看:53
本文介绍了“喜欢"、“不喜欢"导轨插件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何喜欢"、不喜欢"的 Rails 插件...

Is there any "like" , "dislike" plugin for rails...

我浏览了评级插件...但它们都是 5 星级评级插件...

I went through rating plugins... but all of them were 5 star rating plugins...

推荐答案

我建议通过采用经典的投票模型功能来创建 likedislike 选项.

I recommend creating the like and dislike option by taking on the classic vote model functionality.

所以你有 Vote 作为 UserVotable 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.

这篇关于“喜欢"、“不喜欢"导轨插件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆