Rails 5,Undefined method`for'for#<Devise on line devise_parameter_sanitizer。

我正在使用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 for#你的意思是? 叉子):

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)

谁可以帮忙? 怎么解决这个问题?

Dmitriy asked 2019-09-11T11:04:37Z
5个解决方案
156 votes

According to the documentation:

Devime 4的参数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
Зелёный answered 2019-09-11T11:05:02Z
31 votes

如果您只是将.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.

Brandy Burdick answered 2019-09-11T11:05:34Z
2 votes

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

Dmitry Polyakovsky answered 2019-09-11T11:06:00Z
0 votes

我想您在控制器的configure_permitted_parameters方法中错过了account_update,您需要遵循设计模式。 Devise有一个帐户更新页面。 你可以在views / devise / registrations / edit.html.erb中找到它,你的代码也不能在sign_up页面中工作,在这里你指定了sign_up页面

要更新用户表,在用户/编辑中提交更新的时间,或者如果要在sign_up页面中提交用户名,则需要遵循此设计模式,以更新数据库用户表。 即使您向用户表添加了新列,也必须将其添加到configure_permitted_parameters方法中。 在你的情况下它是用户名,但你也错过了account_update。 您基本上是说要更新用户名或将字符串添加到用户名字段而不遵循设计模式。 您添加到User表的任何字段都应遵循此Devise模式。 您还可以指定允许哪个页面更新此用户名。 在下面的示例中,我正在使用设计更新页面。 就像我说的那样,即使您向Users表添加了自定义字段名称,也需要遵循此模式。 如果你有另一个页面需要添加用户名,你就会做同样的事情。

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])
    devise_parameter_sanitizer.permit(:account_update, keys: [:username])
  end
end

接下来请确保在您的user.rb中您已在用户模型中验证用户名。

class User < ApplicationRecord
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable

  validates :username, presence: true
end
Elias Glyptis answered 2019-09-11T11:06:41Z
-1 votes
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
Elias Glyptis answered 2019-09-11T11:06:59Z
translate from https://stackoverflow.com:/questions/37341967/rails-5-undefined-method-for-for-devise-on-line-devise-parameter-sanitizer