我正在使用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'
想法?
顺便说一下,不确定这是否相关,但我有两个模型Client
和User
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
答案 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添加更多动作,这样就可以实际调用其中一个。