所以我真的认为这将有助于创建我们在 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
我尝试按照这个问题中第一个答案的示例进行操作:为什么这个 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
这是我的视频投票模型:
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
如果需要,这是vote_sum
我的视频模型中的方法:
def vote_sum
video_votes.sum(:value)
end