Ruby-on-rails Rails 5,未定义的方法 `for' for #<Devise on line devise_parameter_sanitizer.for

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37341967/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 00:25:00  来源:igfitidea点击:

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

ruby-on-railsdeviseruby-on-rails-5

提问by Dmitriy

I am working with Rails 5

我正在使用 Rails 5

I aded new field username in model User.

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

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

During registration is displayed error: undefined method `for' for # Did you mean? fork

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

Trace:

痕迹:

NoMethodError (undefined method `for' for # Did you mean? fork):

NoMethodError (undefined method `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)

Who can help? How solve this problem?

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

回答by Зелёный

According to the documentation:

According to the documentation:

The Parameter Sanitaizer API has changed for Devise 4

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

回答by Brandy Burdick

If you just change the .forto .permitit works as well. For example:

如果你只是改变它.for.permit它也能工作。例如:

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

It works in both Rails 4.2.x and Rails 5.0.x

它适用于 Rails 4.2.x 和 Rails 5.0.x

回答by Dmitry Polyakovsky

Don't forget devise_parameter_sanitizer.permit(:account_update, keys: [:username])

不要忘记 devise_parameter_sanitizer.permit(:account_update, keys: [:username])

回答by Elias Glyptis

I think you missed account_update in your controller's configure_permitted_parameters method, you need to follow the devise pattern. Devise has a an account update page. You can find this in views/devise/registrations/edit.html.erb, and your code is also not going to work in the sign_up page, here you specified sign_up page

我认为您在控制器的 configure_permitted_pa​​rameters 方法中错过了 account_update,您需要遵循设计模式。Devise 有一个账户更新页面。您可以在 views/devise/registrations/edit.html.erb 中找到它,并且您的代码在 sign_up 页面中也不起作用,这里您指定了 sign_up 页面

To update your user table, the minute you submit an update in your users/edit, or if you are submitting a username in the sign_up page you need to follow this devise pattern, to update the database User table. Even if you added a new column to the user table, you would have to add it to the configure_permitted_parameters method. In your case it's username, but you missed account_update as well. You're basically saying that you want to update the username or add the string to username field without following the Devise pattern. Any field you add to the User table should follow this Devise pattern. Also you can specify which page is permitted to update this username. In my example below, i'm using the devise update page. So like I said, even if you added a custom field name to Users table you need to follow this pattern. If you have another page where you need to add username, you would just do the same thing.

要更新您的用户表,您在用户/编辑中提交更新的那一刻,或者如果您在注册页面中提交用户名,您需要遵循此设计模式来更新数据库用户表。即使您向用户表中添加了一个新列,您也必须将其添加到 configure_permitted_pa​​rameters 方法中。在您的情况下,它是用户名,但您也错过了 account_update 。您基本上是说要更新用户名或将字符串添加到用户名字段而不遵循设计模式。您添加到用户表的任何字段都应遵循此设计模式。您还可以指定允许哪个页面更新此用户名。在下面的示例中,我使用的是设计更新页面。所以就像我说的,即使您向用户表添加了自定义字段名称,您也需要遵循此模式。

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

Next make sure in your user.rb you have validate username in your User model.

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

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

  validates :username, presence: true
end

回答by Elias Glyptis

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