gpt4 book ai didi

ruby-on-rails - rails : How to install CanCan?

转载 作者:数据小太阳 更新时间:2023-10-29 08:51:27 26 4
gpt4 key购买 nike

我已经安装了 CanCan 并将行 gem '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

ActiveAdminDevise 已安装并插入。

我添加了 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)

我该如何解决?

最佳答案

cancan 方法被添加到所有 Controller 和模型,而不是其他任何东西,因此您需要在 ActiveAdmin 类中手动包含 cancan 方法。考虑遵循此处的模式:

https://github.com/ryanb/cancan/blob/master/lib/cancan/controller_additions.rb

使用类似include CanCan::ControllerAdditions

关于ruby-on-rails - rails : How to install CanCan?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11908881/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com