2015-05-27 22 views
1

嗨,我第一次使用康康宝石。我已经包含这在我的宝石文件在滑轨4中使用康康宝石时面临的问题

gem 'cancancan', '~> 1.10' 

然后进行捆绑安装和运行命令轨摹康康舞:通过它产生我ability.rb中,我已经给这个代码

class Ability 
    include CanCan::Ability 

    def initialize(employee) 
    employee ||= Employee.new 
    if employee[:role]== 'HR' 
     can :manage, :all 
    else 
     can :read, :all 
    end 
    end 
end 
能力

,并给我的application_Controller这

rescue_from CanCan::AccessDenied do |exception| 
    flash[:error] = "Access denied." 
    redirect_to root_url 
end 

现在我有员工登录[:作用] =“人力资源”,当我将CR然后eate部门它给了我错误

NameError (undefined local variable or method `current_user' for #<DepartmentsController:0xb48a0c0>): 
    vendor/ruby/2.1.0/gems/cancancan-1.10.1/lib/cancan/controller_additions.rb:357:in `current_ability' 
    vendor/ruby/2.1.0/gems/cancancan-1.10.1/lib/cancan/controller_resource.rb:215:in `current_ability' 
    vendor/ruby/2.1.0/gems/cancancan-1.10.1/lib/cancan/controller_resource.rb:98:in `initial_attributes' 
    vendor/ruby/2.1.0/gems/cancancan-1.10.1/lib/cancan/controller_resource.rb:91:in `assign_attributes' 
    vendor/ruby/2.1.0/gems/cancancan-1.10.1/lib/cancan/controller_resource.rb:86:in `build_resource' 
    vendor/ruby/2.1.0/gems/cancancan-1.10.1/lib/cancan/controller_resource.rb:66:in `load_resource_instance' 
    vendor/ruby/2.1.0/gems/cancancan-1.10.1/lib/cancan/controller_resource.rb:32:in `load_resource' 
    vendor/ruby/2.1.0/gems/cancancan-1.10.1/lib/cancan/controller_resource.rb:25:in `load_and_authorize_resource' 
    vendor/ruby/2.1.0/gems/cancancan-1.10.1/lib/cancan/controller_resource.rb:10:in `block in add_before_filter' 

enter image description here

请指导我如何解决这个问题。提前致谢。

+0

你有设计为用户或员工的current_ability方法?还发布您的设计路线 –

+0

没有使用authlogic – railslearner

+0

您必须具有application_controller中定义的current_user helper方法。对? –

回答

1

你必须覆盖application_controller.rb

def current_ability 
    @current_ability ||= Ability.new(your_current_logged_in_employer_object) 
end