Ruby rails 用户角色使用枚举始终保持为零
Ruby on rails user role always remains nil using enum
我正在尝试在设计中添加多个用户角色功能。我正在为不同的角色使用枚举,但不知何故,在新用户注册后,用户角色始终保持为 nil。
这是我的实现
用户模型
class User < ApplicationRecord
rolify
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable
enum role: { student: 0, assistant: 1, teacher: 2}
end
我还在注册控制器的强参数中添加了角色
registration_controller
class RegistrationsController < Devise::RegistrationsController
private
def sign_up_params
params.require(:user).permit(:username, :email, :password, :password_confirmation, keys: [:role])
end
def account_update_params
params.require(:user).permit(:username, :email, :password, :password_confirmation, :current_password, keys: [:role])
end
end
查看
<%= f.select :role, User.roles %>
我想要的是新用户的角色应该是 he/she 在注册时从下拉列表中选择的任何角色
但是它的角色在注册后总是设置为nil。有人可以解释一下如何解决这个问题
我已经阅读了很多答案并在强参数中添加了 key: [:role]
但仍然无法正常工作
谢谢
如果您打算使用 Rolify,您应该删除该枚举列。
class User < ApplicationRecord
rolify
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable
end
Rolify 使用两个表(roles 和 users_roles)来存储用户的角色。这允许一个用户拥有多个角色,而一个角色可以拥有多个用户。
虽然您可以基于枚举列创建自己的原始角色系统,但两者同时存在肯定会造成混淆,因为 .has_role?
等 Rolify 方法不会考虑您的枚举列。
如果您想让用户 select 角色具有 rolify,您可以这样做:
<%= f.collection_select :role_ids,
Role.where(name: ['student', 'assistant', 'teacher']), :id, :name %>
class RegistrationsController < Devise::RegistrationsController
private
def sign_up_params
params.require(:user).permit(:username, :email, :password, :password_confirmation, role_ids: [])
end
def account_update_params
params.require(:user).permit(:username, :email, :password, :password_confirmation, :current_password, role_ids: [])
end
end
我正在尝试在设计中添加多个用户角色功能。我正在为不同的角色使用枚举,但不知何故,在新用户注册后,用户角色始终保持为 nil。
这是我的实现
用户模型
class User < ApplicationRecord
rolify
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable
enum role: { student: 0, assistant: 1, teacher: 2}
end
我还在注册控制器的强参数中添加了角色
registration_controller
class RegistrationsController < Devise::RegistrationsController
private
def sign_up_params
params.require(:user).permit(:username, :email, :password, :password_confirmation, keys: [:role])
end
def account_update_params
params.require(:user).permit(:username, :email, :password, :password_confirmation, :current_password, keys: [:role])
end
end
查看
<%= f.select :role, User.roles %>
我想要的是新用户的角色应该是 he/she 在注册时从下拉列表中选择的任何角色 但是它的角色在注册后总是设置为nil。有人可以解释一下如何解决这个问题
我已经阅读了很多答案并在强参数中添加了 key: [:role]
但仍然无法正常工作
谢谢
如果您打算使用 Rolify,您应该删除该枚举列。
class User < ApplicationRecord
rolify
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable
end
Rolify 使用两个表(roles 和 users_roles)来存储用户的角色。这允许一个用户拥有多个角色,而一个角色可以拥有多个用户。
虽然您可以基于枚举列创建自己的原始角色系统,但两者同时存在肯定会造成混淆,因为 .has_role?
等 Rolify 方法不会考虑您的枚举列。
如果您想让用户 select 角色具有 rolify,您可以这样做:
<%= f.collection_select :role_ids,
Role.where(name: ['student', 'assistant', 'teacher']), :id, :name %>
class RegistrationsController < Devise::RegistrationsController
private
def sign_up_params
params.require(:user).permit(:username, :email, :password, :password_confirmation, role_ids: [])
end
def account_update_params
params.require(:user).permit(:username, :email, :password, :password_confirmation, :current_password, role_ids: [])
end
end