每当创建新用户时,该用户就会应用一个角色。这是通过after_create方法实现的。方法如下:
def add_user_role
self.roles << Role.find_by_role("user")
end
但是,用户两次获得该角色。创建之后,会发生以下情况:
=> #<ActiveRecord::Associations::CollectionProxy [#<Role id: 21, role: "user", created_at: "2016-10-27 15:13:44", updated_at: "2016-10-27 15:13:44">, #<Role id: 21, role: "user", created_at: "2016-10-27 15:13:44", updated_at: "2016-10-27 15:13:44">]>
irb(main):002:0>
如果我注释掉add命令,则不会对用户应用任何角色:
def add_user_role
# self.roles << Role.find_by_role("user")
end
创建后:
=> #<ActiveRecord::Associations::CollectionProxy []>
有谁知道,为什么会这样?
after_create方法位于User.rb中:
class User < ApplicationRecord
after_create :add_user_role
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :directions
has_many :roles, through: :directions
end
private
def add_user_role
self.roles << Role.find_by_role("user")
end
我有两个用户控制器,它们是:
管理员的用户控制器:
class UsersController < ApplicationController
before_action :authenticate_user!
before_action :current_user_allowed?
def edit
@user = User.find(params[:id])
end
def update
@user = User.find(params[:id])
role = params[:role]
add_user_role(@user,role)
redirect_to root_path
end
end
private
def current_user_allowed?
current_user.roles.each do |role|
if role.role == "superadmin"
return
end
end
redirect_to root_path
end
def add_user_role(user, role1)
user.roles.create(role: role1)
end
def user_params
params.require(:user).permit(:role)
end
控制器由设计:
class Users::RegistrationsController < Devise::RegistrationsController
# before_action :configure_sign_up_params, only: [:create]
# before_action :configure_account_update_params, only: [:update]
# GET /resource/sign_up
# def new
# super
# end
# POST /resource
#def create
# super
#end
# GET /resource/edit
# def edit
# super
# end
# PUT /resource
#def update
# super
#end
# DELETE /resource
# def destroy
# super
# end
# GET /resource/cancel
# Forces the session data which is usually expired after sign
# in to be expired now. This is useful if the user wants to
# cancel oauth signing in/up in the middle of the process,
# removing all OAuth session data.
# def cancel
# super
# end
# protected
# If you have extra params to permit, append them to the sanitizer.
# def configure_sign_up_params
# devise_parameter_sanitizer.permit(:sign_up, keys: [:attribute])
# end
# If you have extra params to permit, append them to the sanitizer.
# def configure_account_update_params
# devise_parameter_sanitizer.permit(:account_update, keys: [:attribute])
# end
# The path used after sign up.
# def after_sign_up_path_for(resource)
# super(resource)
# end
# The path used after sign up for inactive accounts.
# def after_inactive_sign_up_path_for(resource)
# super(resource)
# end
end
在这里,您需要做两件事来创建相同的角色。
在users_controller中:
def add_user_role(user, role1)
user.roles.create(role: role1)
end
然后在user.rb中:
after_create :add_user_role
我建议让控制器处理此问题,并避免在user.rb
类中对模型进行任何修改。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句