Where is user.admin? defined in rails-devise-pundit starter app?

后端 未结 2 1504
梦如初夏
梦如初夏 2021-01-20 06:02

I used RailsApps rails-composer to create a rails-devise-pundit starter application. I am still a little new to ruby on rails and newer to devise, pundit and rails 4.

<
相关标签:
2条回答
  • 2021-01-20 06:23

    Roles are defined in the app/models/User.rb file (the User model).

    class User < ActiveRecord::Base
      .
      .
      .
      enum role: [:user, :vip, :admin]
      after_initialize :set_default_role, :if => :new_record?
    
      def set_default_role
        self.role ||= :user
      end
    
    end
    

    The application uses the ActiveRecord enum method to manage roles. ActiveRecord provides convenient methods to query the role attribute:

    user.admin! # sets the role to "admin"
    user.admin? # => true
    user.role  # => "admin"
    

    See documentation for ActiveRecord::Enum for details. The ActiveRecord enum method is new in Rails 4.1.

    I've updated the README for the rails-devise-pundit application to include this information. It's also covered in my Rails Pundit Tutorial.

    0 讨论(0)
  • 2021-01-20 06:30

    It is an attribute of the User model. Same as first_name or last_name, there is a field called admin which is a boolean.

    0 讨论(0)
提交回复
热议问题