(nil:NilClass 的未定义方法"vote_exclusively_for")当用户注销时,但不在登录时 - Rails 3 (thumbs_up gem)



我正在使用gem thumbs_up

我的上传模型具有所需的代码:

class Upload < ActiveRecord::Base
    acts_as_voteable

进行投票的模型,客户端,也有另一面:

class Client < ActiveRecord::Base
    acts_as_voter

我的uploads_controller如下所示:

class UploadsController < ApplicationController
    before_filter :authenticate_user!, :except => [:vote_up, :vote_down]
def vote_up
        begin
           (current_user || @client_user).vote_exclusively_for(@upload = Upload.find(params[:id]))
          render :nothing => true, :status => 200
        rescue ActiveRecord::RecordInvalid
          render :nothing => true, :status => 404
        end
      end
        def vote_down
            begin
             (current_user || @client_user).vote_exclusively_against(@upload = Upload.find(params[:id]))
                render :nothing => true, :status => 200
            rescue ActiveRecord::RecordInvalid
                render :nothing => true, :status => 404
            end
        end
end

我得到的错误是:

NoMethodError (undefined method `vote_exclusively_for' for nil:NilClass):
  app/controllers/uploads_controller.rb:75:in `vote_up'

思潮?

顺便说一句,不确定这是否相关,但我有两个模型ClientUser acts_as_voter.

编辑1:带@client_user的应用程序控制器

class ApplicationController < ActionController::Base
    def check_token_or_user
        stage = Stage.find(params[:id])
        client = stage.client
        client_access_key = client.authentication_token
        stage_access_key = stage.authentication_token
            if !user_signed_in? && params[:ck].blank? && params[:sk].blank?
          redirect_to(:login)
        elsif !user_signed_in? && params[:ck] == client_access_key && params[:sk] == stage_access_key
          # do nothing, they can view the page      
            elsif user_signed_in?
          # do nothing, they can view the page
            else
                redirect_to(root_path)
        end
    @client_user = Client.where(:authentication_token => client_access_key).first #have to specify which record, otherwise it will save the collection of users, hence the '.first'
    end
end

这在 stages_controller 之前的过滤器中调用,如下所示:

before_filter :check_token_or_user, :only => :compare
在我看来

,current_user和@client_user都是零。

@client_user永远不会定义,因为方法 check_token_or_user 仅在操作 :compare 上调用,而不是在操作 :vote_up 或 :vote_down 上调用

关于current_user - 这取决于您的身份验证是如何实现的,但我想说,如果不调用authenticate_user!,它也将是零。还有authenticate_user!绝对没有被调用,因为before_filter中的 :except => [:vote_up, :vote_down]。

编辑:因此,要解决此问题,您必须向其中一个before_filters添加更多操作,以便其中一个实际上可以被调用。

相关内容

  • 没有找到相关文章

最新更新