Ruby on rails 操作前检查Rails是否为管理员
我安装了Desive,并在用户表中添加了一个记录或列(我不知道如何调用它)admin:boolean,默认情况下为false 在my routes.rb中,我创建了这个/admin链接Ruby on rails 操作前检查Rails是否为管理员,ruby-on-rails,Ruby On Rails,我安装了Desive,并在用户表中添加了一个记录或列(我不知道如何调用它)admin:boolean,默认情况下为false 在my routes.rb中,我创建了这个/admin链接 get 'admin' => 'admin#index' 我不知道如何显示它只为管理员 class AdminController < ApplicationController before_action: I have no idea what to write here def in
get 'admin' => 'admin#index'
我不知道如何显示它只为管理员
class AdminController < ApplicationController
before_action: I have no idea what to write here
def index
end
end
class AdminController
在控制器中尝试以下操作:
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
class AdminController
谢谢,is_admin方法是否应该在private中?是的,您可以在private中设置它。您救了我一天,所以您打赌我会接受它,非常感谢!此答案假设用户已登录。您应该在\u操作:验证\u用户之前添加前一行中的code>以捕获用户未登录时的情况。