我已经安装了 CanCangem 'cancan'
并向 Gemfile 添加了行。冉bundle
。
我app/models/ability.rb
用代码创建:
class Ability
include CanCan::Ability
def initialize(user)
user ||= User.new # guest user (not logged in)
if user.admin?
can :manage, :all
end
end
end
ActiveAdmin和Devise已安装并插入。
我添加if cannot?
到 app/admin/codes.rb:
ActiveAdmin.register Code do
if cannot? :manage, Code
flash[:notice] = "Access denied"
redirect_to '/'
end
index do
column :id
column :title
column :author
column :created_at
column :updated_at
column :language
default_actions
end
end
rails 无法启动服务器或打开 rails 控制台:
app/admin/codes.rb:2:in `block in <top (required)>': undefined method `cannot?'
for #<ActiveAdmin::ResourceDSL:0x4ae7fe8> (NoMethodError)
我该如何解决?