Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails (未定义的方法'vote_exclusive_for';for nil:NilClass)在用户注销时,但在登录时不使用-Rails 3(竖起拇指)_Ruby On Rails_Ruby On Rails 3 - Fatal编程技术网

Ruby on rails (未定义的方法'vote_exclusive_for';for nil:NilClass)在用户注销时,但在登录时不使用-Rails 3(竖起拇指)

Ruby on rails (未定义的方法'vote_exclusive_for';for nil:NilClass)在用户注销时,但在登录时不使用-Rails 3(竖起拇指),ruby-on-rails,ruby-on-rails-3,Ruby On Rails,Ruby On Rails 3,我正在使用gem竖起大拇指 我的上传模型具有所需的代码: class Upload < ActiveRecord::Base acts_as_voteable 我得到的错误是: NoMethodError (undefined method `vote_exclusively_for' for nil:NilClass): app/controllers/uploads_controller.rb:75:in `vote_up' 想法 顺便说一句,我不确定这是否相关,但我有

我正在使用
gem竖起大拇指

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

class Upload < ActiveRecord::Base
    acts_as_voteable
我得到的错误是:

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

顺便说一句,我不确定这是否相关,但我有两种型号的
客户端
用户
,它们都有
作为投票人

Edit1:具有
@client\u 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

在我看来,当前用户和@client用户都是零

@从未定义客户端用户,因为只在操作:比较时调用方法check\u token\u或\u user,而不是在操作:投票赞成或投票反对时调用

关于当前用户-这取决于您的身份验证是如何实现的,但我想说,如果您对用户进行身份验证!如果不调用,它也将为零。并验证您的用户!绝对不会调用,因为before\u筛选器中的:except=>[:vote\u up,:vote\u down]


编辑:所以要解决这个问题,你必须在过滤器之前向其中一个添加更多操作,这样其中一个就可以被调用。

在我的应用程序控制器中。基本上,我检查URL中是否有某些参数,如果有,我将
@client\u user
设置如下:
@client\u user=client.where(:authentication\u token=>client\u access\u key)。首先,
…其中,
客户端访问键
是URL中的一个参数-实际上,它是存储在局部变量中的URL值…但是你明白了。Hrmm…当我在页面上放置
调试(@client\u user)
时,它向我显示了正确的@client\u用户。我用@client\u用户代码为我的应用程序控制器更新了问题代码段。如果我告诉你,正在呈现的操作(即,
上传
可以投票的位置)正在被调用。这会改变你的答案吗?如中所示,url为
stages/46/compare?p1=jsugkjeb
,其中针对特定操作存在
before\u filter
,例如:
before\u filter:check\u token\u或\u user,:only=>:compare
因此,假设基本上是因为它们已经在
compare
操作中(因为这是唯一允许投票的操作)应该初始化
@client\u user
而不是nil。另外,我添加了一个
debug(@client\u user)
,看到
@client\u user
!=nil例如,这是我的
debug(@client\u user)的输出
--!ruby/object:Client属性:id:62电子邮件:test@abc.com用户id:1创建时间:2011-04-06 10:14:12.002169更新时间:2011-04-06 10:14:12.002169身份验证令牌:rRC58oHa48WZlZFO2Fr_u2;属性缓存:{}
,这是正确的。伙计,您显示的错误消息清楚地表明(current_user | |@client_user)为nil,因为您得到消息:nil:NilClass的未定义方法'vote_exclusive_for'。那么current_user和@client_user都必须为nil。请确保将debug命令放置在正确的位置,以便调用vote_up操作。请注意,无论渲染什么,“当前操作”是在控制器中被调用的方法,无论你在控制器中渲染什么。好的……谢谢……你的回答帮助我缩小了范围并得到了它。我最终不得不在过滤器之前写一个新的
,但你确实帮了我缩小了范围。
NoMethodError (undefined method `vote_exclusively_for' for nil:NilClass):
  app/controllers/uploads_controller.rb:75:in `vote_up'
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
before_filter :check_token_or_user, :only => :compare