[英](undefined method `vote_exclusively_for' for nil:NilClass) when user is logged out, but not when logged in - 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'
有什么想法吗?
顺便说一句,不确定这是否相关,但是我有两个具有acts_as_voter
模型Client
和User
。
编辑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过滤器中调用此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之一添加更多操作,以便实际上可以调用其中之一。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.