(用户注销时未定义的方法`vote_exclusively_for'为nil:NilClass)但登录时没有--Rails 3(thumbs_up gem)

时间:2011-05-10 04:12:49

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

我正在使用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

Edit1:带有@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

1 个答案:

答案 0 :(得分:1)

在我看来,current_user和@client_user都是nil。

永远不会定义@client_user,因为方法check_token_or_user只在动作时被调用:比较,而不是你的动作:vote_up或:vote_down

关于current_user - 它取决于您的身份验证的实现方式,但我会说,如果是authenticate_user!没有被称为,它也将是零。和authenticate_user!绝对没有被调用,因为:except =&gt;在before_filter中有[:vote_up,:vote_down]。

编辑:所以要解决这个问题,你必须向其中一个before_filters添加更多动作,这样就可以实际调用其中一个。

相关问题