“Like”, “Dislike” plugin for rails

冷暖自知 提交于 2019-12-17 23:23:49

问题


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

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


回答1:


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

So you have Vote as a join table between the User and the Votable Item .

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.




回答2:


I know an answer has already been accepted but I've just found a gem called act_as_votable that might fit your needs:

Acts As Votable is a Ruby Gem specifically written for Rails/ActiveRecord models. The main goals of this gem are:

  • Allow any model to be voted on, like/dislike, upvote/downvote, etc.
  • Allow any model to vote. In other words, votes do not have to come from a user, they can come from any model (such as a Group or Team).
  • Provide an easy to write/read syntax.



回答3:


Try Recommendable:

https://github.com/davidcelis/recommendable




回答4:


I see nothing wrong with the question.

Up down voting is still rating. Just using only two stars. :) I seem to remember acts_as_rateable allowing number of star definition.




回答5:


Why don't just create a new boolean field named like? and then update it as false for dislike and true for like. I doubt you find a plugin/gem for this functionality.



来源:https://stackoverflow.com/questions/3703194/like-dislike-plugin-for-rails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!