我找不到devise_parameter_sanitizer.sanitize()方法的用法。请帮助我理解它的用法,以及它与devise_parameter_sanitizer.for()方法的区别。
发布于 2020-12-04 14:09:16
devise使用the Devise::ParameterSanitizer class中定义的devise_parameter_sanitizer.sanitize()方法来过滤给定操作的控制器中允许的参数。
它与Rails的strong parameters特性非常相似。
您可以使用它来允许比devise gem定义的默认字段更多的字段。
在编写本文时,the gem documentation中描述的缺省操作->属性如下所示
DEFAULT_PERMITTED_ATTRIBUTES = {
sign_in: [:password, :remember_me],
sign_up: [:password, :password_confirmation],
account_update: [:password, :password_confirmation, :current_password]
}基本上,作为shown in the gem's documentation permit usage examples,您传递要更改其允许属性的:action,然后使用字段列表对其调用permit():
# Adding new parameters to be permitted in the `sign_up` action.
devise_parameter_sanitizer.permit(:sign_up, keys: [:subscribe_newsletter])
# Removing the `password` parameter from the `account_update` action.
devise_parameter_sanitizer.permit(:account_update, except: [:password])
# Using the block form to completely override how we permit the
# parameters for the `sign_up` action.
devise_parameter_sanitizer.permit(:sign_up) do |user|
user.permit(:email, :password, :password_confirmation)
end还可以看看创业板的explains pretty well all of this的README.md。
https://stackoverflow.com/questions/45115944
复制相似问题