简体   繁体   中英

Add an Admin Role to the Devise's User Model

I think my question is simple, I followed step by step the second option that appears on this page How To: Add an Admin Role . Now I would like to use current_user.admin? with the before_action method (thus before_action :current_user.admin? ), but I see this error.

**undefined method `admin?' for :current_user:Symbol**

I can't even use the method admin with the before_action method, and if you are wondering, yes, I'm logged.

Now, what I can do is define a new method in the controller, such as admin_only thus.

  def admin_only
    if !current_user.admin then
      redirect_to :back, :alert => "Access denied."
    end
  end

And then use before_action :admin_only and it works well, but I think it breaks a little the logic of the controller to define a method such as admin_only. So... is there a way to filter administrators without having to define new methods in the controller?.

You should define a method to do that. Because before_action need you to have an defined action, and tell rails to call it by pass its name as a symbol.

And that's why you get an undefined method error, cause :current_user is just a symbol, not the user object you expected.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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