尝试使用未定义的current_user

时间:2011-04-18 05:02:12

标签: ruby-on-rails ruby ruby-on-rails-3 model undefined

我在投票模型中有这个电话:

 fires :vote_updated, :on => :update,
                   :actor => :user,
                   :secondary_subject => :video,
                   :if => lambda { |vote| ((vote.value == 1) || (vote.value == -1)) && (vote.video.user != current_user)}

如果你不熟悉,它适用于timeline_fu plugin

如果拥有已投票视频的用户是当前用户,我不希望触发此呼叫。这就是这条线的用武之地:

:if => lambda { |vote| ((vote.value == 1) || (vote.value == -1)) && (vote.video.user != current_user)}

但是,我无权访问current_user。我该如何解决这个问题?

这是我的投票控制器中的创建方法(实际上没有更新方法):

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
      format.js
    end
  end  
end

2 个答案:

答案 0 :(得分:0)

我认为正确的做法是在控制器中验证这一点。我会为这种情况创建一个前置过滤器

更新:

就像一个简单的例子:

before_filter :valid_vote, :only => :update

def update
   @vote.update_attributes(params[:vote]) # or whatever
end
..

private

def valid_vote
   @vote = Vote.find params[:id]
   unless ( @vote.video.user.id != current_user.id )
      render :text => 'You can't vote for your own video', :status => 403
   end
end

因此,在执行“更新”操作之前,会声明并验证@vote。 如果它无效,则您的“更新”操作保持不变

更新2:

不确定你会怎么样,但你也可以这样做:

在你的投票模型中:

attr_accessor :skip_timeline

然后在过滤前使用概念,但执行@vote.skip_timeline = true而不是渲染文本

然后声明可能如下所示:

:if => lambda { |vote| ((vote.value == 1) || (vote.value == -1)) && !vote.skip_timeline }

您也可以将((vote.value == 1) || (vote.value == -1))移至您之前的过滤器:

def valid_vote
   @vote = Vote.find params[:id]
   unless ( [1,-1].include? @vote.value && @vote.video.user.id != current_user.id )
       @vote.skip_timeline = true
   end
end

:if => lambda {|投票| !vote.skip_timeline}

答案 1 :(得分:0)

您收到此错误,因为通常不建议您访问模型中的current_user(或会话信息)。我并不熟悉timeline_fu gem,所以这个答案不会是你得到的最好答案。我只是向您展示如何从任何模型访问current_user。

首先转到您的应用程序控制器。您将要创建一个设置当前用户的方法。您需要在之前的过滤器中调用该方法。

before_filter :loadCurrentUser 

def loadCurrentUser
  User.currentUser = current_user
end

然后在您的用户模型中,您需要定义'currentUser'。

def self.currentUser
   Thread.currentUser[:user]
end

您不一定要在应用程序控制器中声明current_user,但由于它是一个gem,我不确定它是否具有易于访问的控制器。

编辑:这种方式可能容易出现问题,但我不完全确定你是否在询问如何在模型中提供current_user,或者是一个完全不同的解决方法,这样你就不会遇到这个问题......并且阅读对另一个答案的回答,我认为这不是你要问的。