0
我最近通過CanCanCan(通過枚舉)將角色添加到我的Rails應用程序 - 但現在我想在註冊時添加默認角色。我如何去做這件事?它是否在控制器或模型?Rails Cancan:在註冊時定義默認角色
我的用戶模型:
class User < ActiveRecord::Base
#Defining different roles
enum role: [:Admin, :User, :Guest]
#Users can only create one scholarship application
has_one :applications
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
end
我的能力模型 - 這裏只有三個角色,我將通過與用戶與1-作用,無慾無求的數據庫中創建一個管理員,然後每個人都應該爲2上註冊:
class Ability
include CanCan::Ability
def initialize(user)
user ||= User.new # guest user (not logged in)
if user.role = 1
can :manage, :all
elsif user.role = 2
can :manage, Application
can :manage, User
else
can :read, Static_Page
end
end
end