Rails 5,未定义方法`for',用于#<Devise在线devise_parameter_sanitizer.for


95

我正在使用Rails 5

我在模型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

注册期间显示错误:#的未定义方法“ for”,是您的意思吗?叉子

跟踪:

NoMethodError(#的未定义方法“ 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)

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


1
它也在Rails 4.2上发生。
sekrett '16

Answers:


173

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

由于某些原因,这可以工作,但是当我重新启动滑轨时,它
又会

@ K2xL清除您的垃圾箱?糟糕,十月。您是如何解决的?
snowYetis

停止+重新启动弹簧,pehaps
phillyslick

1
谢谢!对于那些不想更新其代码的人,您可以相应地调整您的Gemfile- gem 'devise', '~> 3'为我排序。
Darragh Enright

34

如果你只需要改变.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中使用



0

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

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

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

-1
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
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.