本文介绍了为什么不这样的Ruby on Rails code工作,因为我想让它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我试图建立我在这个问题问:修正表决机制

但是,这个方案是行不通的。用户仍然可以投票但很多次,他或她想要的。我怎么能解决这个问题和/或重构?

 高清创建
    @video = Video.find(PARAMS [:VIDEO_ID])
    @vote = @ video.video_votes.new
    @ vote.user = CURRENT_USER

    如果PARAMS [:类型] ==涨
      @ vote.value = 1
    其他
      @ vote.value = -1
    结束

  如果@ previous_vote.nil?
    如果@ vote.save
      respond_to代码做|格式|
        的format.html {redirect_to时@video}
        format.js
      结束
    其他
      respond_to代码做|格式|
        的format.html {redirect_to时@video}
        format.js {渲染fail_create.js.erb'}
      结束
    结束
  ELSIF @ previous_vote.value == PARAMS [:类型]
    @ previous_vote.destroy
  其他
    @ previous_vote.destroy
    如果@ vote.save
      respond_to代码做|格式|
        的format.html {redirect_to时@video}
        format.js
      结束
    其他
      respond_to代码做|格式|
        的format.html {redirect_to时@video}
        format.js {渲染fail_create.js.erb'}
      结束
    结束
  结束
  @ previous_vote = VideoVote.where(:VIDEO_ID => PARAMS [:VIDEO_ID]:USER_ID => current_user.id)。首先
结束
 

解决方案

@ previous_vote 似乎是零,在每个请求的开始?

我个人弄死在控制器中的所有逻辑,并把在模型和数据库级别的唯一性约束上属于他们的地方。

更新

这可能是完全错误的,但把它当作伪code

模式是这样的:

 类视频<的ActiveRecord :: Base的
  的has_many:票
结束

一流的投票<的ActiveRecord :: Base的
  belongs_to的:用户
  belongs_to的:视频
  validates_uniqueness_of:USER_ID,:范围=> :VIDEO_ID#只有每人视频一票
结束

类用户的LT;的ActiveRecord :: Base的
  的has_many:票
结束
 

控制器:

 高清创建
  @video = Video.find(PARAMS [:VIDEO_ID])
  @vote = current_user.votes.find_or_create_by_video_id(@ video.id)

  #所以你赋值表决基础上的任何逻辑,你需要改变code这下块
  如果you_need_to_do_anything_to_change_its_value
    @ vote.value =:任何
  结束

  如果@ vote.save
    redirect_to时@video
  其他
    渲染:whatever_is_appropriate
  结束
结束
 

So I attempted to build what I asked about in this question: Fix voting mechanism

However, this solution doesn't work. A user can still vote however many times he or she wants. How could I fix this and/or refactor?

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

    if params[:type] == "up"
      @vote.value = 1
    else
      @vote.value = -1
    end

  if @previous_vote.nil?
    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
  elsif @previous_vote.value == params[:type]
    @previous_vote.destroy
  else
    @previous_vote.destroy
    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
  @previous_vote = VideoVote.where(:video_id => params[:video_id], :user_id => current_user.id).first
end
解决方案

@previous_vote seems to be nil at the beginning of every request?

I would personally do away with all the logic in the controller and put uniqueness contraints at Model or database level where they belong.

Updated

This is probably full of errors but treat it as pseudo-code

Models something like:

class Video < ActiveRecord::Base
  has_many :votes
end

class Vote < ActiveRecord::Base
  belongs_to :user
  belongs_to :video
  validates_uniqueness_of :user_id, :scope => :video_id # only one vote per person per video
end

class User < ActiveRecord::Base
  has_many :votes
end

Controller:

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

  # change this next block of code so you assign value to the vote based on whatever logic you need
  if you_need_to_do_anything_to_change_its_value
    @vote.value = :whatever
  end

  if @vote.save
    redirect_to @video
  else
    render :whatever_is_appropriate
  end
end

这篇关于为什么不这样的Ruby on Rails code工作,因为我想让它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 16:24