简体   繁体   中英

Devise with roles

I am using devise for authentication, Now in registration steps i want to add extra parameter, so I use devise_parameter_sanitizer like following

def configure_permitted_parameters
  devise_parameter_sanitizer.permit(:sign_up, keys: [:first_name, :last_name, :email, :subdomain, :phone, :company, role_ids: []])
end

If I pass role_ids rails will automatically add roles for user, but I want to pass role_names instead of role_ids but its give me error

ActiveModel::UnknownAttributeError (unknown attribute 'roles_name' for User.)

Edited:

models/user.rb
has_many :roles_user, dependent: :destroy
has_many :roles, through: :roles_user

Is role_names= defined on User ? Do you have a database field or relation for role_names ? Perhaps you simply have to rename role_ids column to role_names in your database.

I fix the issue using foloowing way

def configure_permitted_parameters
  devise_parameter_sanitizer.permit(:sign_up, keys: [:first_name, :last_name, :email, :subdomain, :phone, :company, role_names: []])
end

Add role_names in devise_parameter_sanitizer

And create new method with name role_names= in models/user.rb

def role_names=(role)
  roles = Role.where("name IN (?)", role)
  self.roles << roles
end

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