我有一个标准User
模型,里面有一个admin
布尔值。对于将其设置为 true 的用户来说一切都很好,但对于普通用户,我会收到此错误:
undefined local variable or method `current_user'
app/models/doc.rb:18:in `mine'
app/controllers/docs_controller.rb:9:in `index'
第Doc
18 行的模型如下所示:
def self.mine
where(:user_id => current_user.name, :retired => "active").order('created_at DESC')
end
我的User
模型如下所示:
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable
attr_accessor :current_password
attr_accessible :name, :password, :password_confirmation, :current_password, :email, :remember_me, :admin
end
class Ability
include CanCan::Ability
def initialize(user)
can :manage, :all if user.admin
end
end
在我的应用程序控制器中,我有以下内容:
class ApplicationController < ActionController::Base
protect_from_forgery
after_filter :user_activity
rescue_from CanCan::AccessDenied do |exception|
redirect_to root_path
end
def admin?
self.admin == true
end
def authenticate_admin
redirect_to :new_user_session_path unless current_user && current_user.admin?
end
private
def user_activity
current_user.try :touch
end
end
我认为这一切都是相关的。我这辈子都想不通。