gpt4 book ai didi

ruby-on-rails - Rails 操作前检查是否为管理员

转载 作者:行者123 更新时间:2023-12-03 15:20:21 25 4
gpt4 key购买 nike

我安装了 devise,并在用户表中添加了一条记录或列(我不知道如何调用它)一个 admin:boolean,默认情况下它是 false

在我的 routes.rb 中,我创建了这个/admin 链接

get 'admin' => 'admin#index'

我不知道如何只为管理员显示它

class AdminController < ApplicationController
before_action: I have no idea what to write here

def index
end

end

最佳答案

在您的 Controller 中尝试这样的操作:

class AdminController < ApplicationController
before_action :is_admin?

def index

end


# it will call before every action on this controller
def is_admin?
# check if user is a admin
# if not admin then redirect to where ever you want
redirect_to root_path unless current_user.admin?
end

end

关于ruby-on-rails - Rails 操作前检查是否为管理员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38808539/

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