繁体   English   中英

行动前的Rails检查管理员

[英]Rails before action check if admin

我安装了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

在您的控制器中尝试这样:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM