0
我想檢查登錄用戶是否投了一個特定的職位投票。我想添加一個屬性到Post的JSON屬性來做到這一點。下面是Post模型的相關代碼:檢查當前用戶是否投了thumbs_up寶石
def user_has_voted
if current_user?
self.voted_by(current_user)
end
end
def self.public_models(posts)
posts.to_json({:include => {:user => { :only => [:uid, :email] }, :company => { :only => :name} }, :methods => [:image_url, :total_votes, :user_has_voted]}).html_safe
end
沒有與該方法的問題user_has_voted,因爲我得到以下錯誤:
undefined local variable or method `current_user' for #<Post:0x00000004d8eab0>
我認爲這個問題是當前用戶不是由個別郵政的範圍看。我怎麼能解決這個問題?
問題在於我無法從代碼內部訪問current_user。我試圖使用jbuilder,但我仍然無法訪問current_user。 – egidra 2013-03-12 00:35:14
您是否使current_user成爲輔助方法(假設它是應用程序控制器中的方法)? – Robert 2013-03-12 00:40:02