为什么这个Ruby on Rails投票代码不起作用?

时间:2022-12-19 07:21:36

So I really thought this would work to create the voting system like we have here on SO:

所以我真的认为这可以创建像我们在这里SO上的投票系统:

def create       
  @video = Video.find(params[:video_id])
  @vote = current_user.video_votes.find_or_create_by_video_id(@video.id)

  if @vote.value.nil?
    if params[:type] == "up"
      @vote.value = 1
    else
      @vote.value = -1
    end
  elsif (params[:type] == "up" && @vote.value == 1) || (params[:type] == "down" && @vote.value == -1)
    @vote.value = 0
  elsif ((params[:type] == "up" && @vote.value == -1) || (params[:type] == "down" && @vote.value == 1)) || (@vote.value == 0)
    if params[:type] == "up"
      @vote.value = 1
    else
      @vote.value = -1
    end
  end  

  if @vote.save
    respond_to do |format|
      format.html { redirect_to @video }
      format.js
    end
  else
    respond_to do |format|
      format.html { redirect_to @video }
      format.js {render 'fail_create.js.erb'}
    end
  end   
end

I tried to follow the example of the first answer in this question: Why doesn't this Ruby on Rails code work as I intend it to? My code, however, doesn't let me vote on a video for the first time because of this error:

我试着按照这个问题中第一个答案的例子:为什么这个Ruby on Rails代码不能像我想要的那样工作?但是,由于此错误,我的代码不允许我第一次对视频进行投票:

TypeError (nil can't be coerced into Fixnum):
app/models/video_vote.rb:11:in `update_vote_sum'
app/controllers/video_votes_controller.rb:4:in `create

Here's my videovote model:

这是我的视频投票模型:

class VideoVote < ActiveRecord::Base
  belongs_to :user
  belongs_to :video
  validates_uniqueness_of :user_id, :scope => :video_id

  after_create :update_vote_sum

  private

    def update_vote_sum
      video.update_attributes!(:vote_sum => video.vote_sum + value)
    end
end

And in case this is needed, here's my vote_sum method from my Video model:

如果需要,这是我的视频模型中的vote_sum方法:

def vote_sum
  video_votes.sum(:value)
end

2 个解决方案

#1


3  

In your after_create method in VideoVote.rb, you're summing over all votes, including the one you've just created with a nil value. Switch after_create to after_update, or set a default for value on VideoVote.

在VideoVote.rb中的after_create方法中,您将对所有投票进行求和,包括您刚刚使用nil值创建的投票。将after_create切换为after_update,或在VideoVote上设置值的默认值。

In looking at that, if vote_sum is calling sum on all of your votes every time anyways, you probably don't even need an after_create or after_update method if votes have a default value set.

在这看来,如果vote_sum每次都在你的所有选票上调用sum,那么如果投票设置了默认值,你可能甚至不需要after_create或after_update方法。

--

You could also replace this entire system with the thumbs_up gem, and save yourself some hassle.

您也可以使用thumbs_up gem替换整个系统,并省去一些麻烦。

#2


0  

Not sure what you are trying to do here. vote_sum is a method that sums up the number of votes that a video has, from what I can see, so why are you trying to update it as an attribute? It will always return the correct score of a video, you shouldn't need to manually update it, since it is not an attribute, but a calculated result of a method.

不知道你在这里想做什么。 vote_sum是一种总结视频投票数的方法,从我所看到的,所以你为什么要尝试将其更新为属性?它将始终返回视频的正确分数,您不需要手动更新它,因为它不是属性,而是方法的计算结果。

#1


3  

In your after_create method in VideoVote.rb, you're summing over all votes, including the one you've just created with a nil value. Switch after_create to after_update, or set a default for value on VideoVote.

在VideoVote.rb中的after_create方法中,您将对所有投票进行求和,包括您刚刚使用nil值创建的投票。将after_create切换为after_update,或在VideoVote上设置值的默认值。

In looking at that, if vote_sum is calling sum on all of your votes every time anyways, you probably don't even need an after_create or after_update method if votes have a default value set.

在这看来,如果vote_sum每次都在你的所有选票上调用sum,那么如果投票设置了默认值,你可能甚至不需要after_create或after_update方法。

--

You could also replace this entire system with the thumbs_up gem, and save yourself some hassle.

您也可以使用thumbs_up gem替换整个系统,并省去一些麻烦。

#2


0  

Not sure what you are trying to do here. vote_sum is a method that sums up the number of votes that a video has, from what I can see, so why are you trying to update it as an attribute? It will always return the correct score of a video, you shouldn't need to manually update it, since it is not an attribute, but a calculated result of a method.

不知道你在这里想做什么。 vote_sum是一种总结视频投票数的方法,从我所看到的,所以你为什么要尝试将其更新为属性?它将始终返回视频的正确分数,您不需要手动更新它,因为它不是属性,而是方法的计算结果。