Rails 5,未定义的方法`for'for#<在devise_parameter_sanitizer.for上devise

我正在使用Rails 5

我在模型用户中添加了新的字段用户名。

class Users::RegistrationsController < Devise::RegistrationsController before_action :configure_permitted_parameters protected def configure_permitted_parameters devise_parameter_sanitizer.for(:sign_up).push(:username) end end 

在注册过程中显示错误: 未定义的方法`for'for#你的意思是? 叉子

跟踪:

NoMethodError(未定义的方法`for'for#你的意思?fork):

 app/controllers/users/registrations_controller.rb:7:in `configure_permitted_parameters' Rendering /usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-5.0.0.rc1/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout Rendering /usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-5.0.0.rc1/lib/action_dispatch/middleware/templates/rescues/_source.html.erb Rendered /usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-5.0.0.rc1/lib/action_dispatch/middleware/templates/rescues/_source.html.erb (5.0ms) Rendering /usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-5.0.0.rc1/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb Rendered /usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-5.0.0.rc1/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (2.9ms) Rendering /usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-5.0.0.rc1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb Rendered /usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-5.0.0.rc1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (1.2ms) Rendered /usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-5.0.0.rc1/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (118.1ms) 

谁可以帮忙? 如何解决这个问题?

According to the documentation:

Devise 4的Parameter Sanitaizer API已经改变

 class ApplicationController < ActionController::Base before_action :configure_permitted_parameters, if: :devise_controller? protected def configure_permitted_parameters devise_parameter_sanitizer.permit(:sign_up, keys: [:username]) end end 

如果您只是将.for更改为.permit它也可以。 例如:

 devise_parameter_sanitizer.permit(:sign_up) { |u| u.permit({ roles: [] }, :email, :password, :password_confirmation, :username) } 

它可以在Rails 4.2.x和Rails 5.0.x中使用

别忘了devise_parameter_sanitizer.permit(:account_update, keys: [:username])

或者,使用Devise gem version 4.1:

 gem 'devise', '~> 4.1.0' 
 class ApplicationController < ActionController::Base before_action :configure_permitted_paramters, if: :devise_controller? protected def configure_permitted_paramters devise_parameter_sanitizer.permit(:sign_up, keys: [:fullname]) devise_parameter_sanitizer.permit(:account_update, keys: [:fullname, :phone_number, :description, :email, :password]) end end