如何实现每个用户每个评论一票?

发布于 2024-11-26 02:38:51 字数 618 浏览 1 评论 0原文

我目前有一个评论控制器,它有方法 vote_up 和 vote_down 这就是我的 vote_up 目前的工作方式。

我的评论模型有描述和计数字段。

  def vote_up
    @comment = Comment.find(params[:comment_id])
    @comment.count += 1
    if @comment.save
      flash[:notice] = "Thank you for voting"
      respond_to do |format|
        format.html { redirect_to show_question_path(@comment.question) }
        format.js
      end
    else
      flash[:notice] = "Error Voting Please Try Again"
      redirect_to show_question_path(@comment.question)
    end
  end

这允许多次投票赞成和反对。我将如何设计它,以便用户每个评论只能投票一次,但以某种方式跟踪他们是否投了赞成票或反对票,这样他们就可以根据需要更改投票。

I currently have a comment controller that has the method vote_up and vote_down this is how my vote_up currently works.

My Comment model has description and a count field.

  def vote_up
    @comment = Comment.find(params[:comment_id])
    @comment.count += 1
    if @comment.save
      flash[:notice] = "Thank you for voting"
      respond_to do |format|
        format.html { redirect_to show_question_path(@comment.question) }
        format.js
      end
    else
      flash[:notice] = "Error Voting Please Try Again"
      redirect_to show_question_path(@comment.question)
    end
  end

This allows for multiple vote up and downs. How would I design it so that a user can only vote once per comment but somehow keep track if they voted up or down, so they have the ability to change their vote if they want too.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

此岸叶落 2024-12-03 02:38:51

你可以做这样的事情。它禁止相同的投票,但允许将投票更改为相反的投票(这是一个赞成/反对系统)。

def vote(value, user) # this goes to your model

  #find vote for this instance by the given user OR create a new one
  vote = votes.where(:user_id => user).first || votes.build(:user_id => user)

  if value == :for
    vote_value = 1
  elsif value == :against
    vote_value = -1
  end

  if vote.value != vote_value
    vote.value = vote_value
    vote.save
  end
end

迁移:

def self.up
    create_table :votes do |t|
    t.references :comment, :null => false
    t.references :user, :null => false
    t.integer :value, :null => false
  end
  add_index :votes, :post_id
  add_index :votes, :user_id
  add_index :votes, [:post_id, :user_id], :unique => true
end

或者,您可以使用名为 thumbs_up 或任何其他的 gem。

You could do something like this. It prohibits identical votes but allows changing the vote to the opposite (it's a thumbs up/thumbs down system).

def vote(value, user) # this goes to your model

  #find vote for this instance by the given user OR create a new one
  vote = votes.where(:user_id => user).first || votes.build(:user_id => user)

  if value == :for
    vote_value = 1
  elsif value == :against
    vote_value = -1
  end

  if vote.value != vote_value
    vote.value = vote_value
    vote.save
  end
end

migration:

def self.up
    create_table :votes do |t|
    t.references :comment, :null => false
    t.references :user, :null => false
    t.integer :value, :null => false
  end
  add_index :votes, :post_id
  add_index :votes, :user_id
  add_index :votes, [:post_id, :user_id], :unique => true
end

Alternatively, you could use a gem called thumbs_up or any other.

清秋悲枫 2024-12-03 02:38:51
class AnswersController < ApplicationsController
  def vote
    #params[:answer_id][:vote]
    #it can be "1" or "-1"
    @answer = Answer.find(params[:answer_id])
    @answer.vote!(params[:answer_id][:vote])
  end

  def show
    @answer = Answer.find(params[:answer_id])
    @answer.votes.total_sum
  end

end

class Answer < ActiveRecord::Base
  has_many :votes do
    def total_sum
      votes.sum(:vote)
    end
  end


  def vote!(t)
    self.votes.create(:vote => t.to_i)
  end

end

class Vote < ActiveRecord::Base
  belongs_to :answer
  belongs_to :user

  validates_uniqueness_of :user_id, :scope => :answer_id
end
class AnswersController < ApplicationsController
  def vote
    #params[:answer_id][:vote]
    #it can be "1" or "-1"
    @answer = Answer.find(params[:answer_id])
    @answer.vote!(params[:answer_id][:vote])
  end

  def show
    @answer = Answer.find(params[:answer_id])
    @answer.votes.total_sum
  end

end

class Answer < ActiveRecord::Base
  has_many :votes do
    def total_sum
      votes.sum(:vote)
    end
  end


  def vote!(t)
    self.votes.create(:vote => t.to_i)
  end

end

class Vote < ActiveRecord::Base
  belongs_to :answer
  belongs_to :user

  validates_uniqueness_of :user_id, :scope => :answer_id
end
情话已封尘 2024-12-03 02:38:51

您也许可以在模型中添加验证,以确保计数在数值上等于或小于 1

validates :count, :numericality => { :less_than_or_equal_to => 1 }

you could perhaps add a validation in your model to make sure that count is numerically equal to or less than 1

validates :count, :numericality => { :less_than_or_equal_to => 1 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文