简体   繁体   English

不允许的参数将新字段添加到Rails中的Devise中

[英]Unpermitted Parameters adding new fields to Devise in rails

when a user make a sign up and press sign up i get this error 当用户进行注册并按注册时,出现此错误

undefined method `for' for #<Devise::ParameterSanitizer:0x007fe4c2821988> Did you mean? fork

  }
elsif params[:action] == 'create'
  devise_parameter_sanitizer.for(:sign_up) { 
    |u| u.permit(registration_params) 
  }
end

this is my application controller 这是我的应用程序控制器

class ApplicationController < ActionController::Base
  protect_from_forgery with: :exception
  before_action :configure_devise_permitted_parameters, if: :devise_controller?

  protected
  def configure_devise_permitted_parameters
    registration_params = [
       :first_name, :last_name, :address, :gender, :DOB, 
       :email, :password, :password_confirmation, 
       :image, :thumb, :medium
    ]

    if params[:action] == 'update'
      devise_parameter_sanitizer.for(:account_update) { 
        |u| u.permit(registration_params << :current_password)
      }
    elsif params[:action] == 'create'
      devise_parameter_sanitizer.for(:sign_up) { 
        |u| u.permit(registration_params) 
      }
    end
  end
end

can i know what is the problem and how to solve it 我能知道问题是什么以及如何解决

In order to fix this problem, replace for with permit in your code. 为了解决这个问题,更换forpermit在你的代码。

Explanation: 说明:

The method #for on Devise::ParamsSanitizer has been removed in devise 4.2, and replaced with two new methods #sanitize and #permit . Devise :: ParamsSanitizer上的#for方法已在设计4.2中删除, #sanitize#permit两个新方法#permit

#permit takes a block argument, so it is probably what you want to use here. #permit采用块参数,因此可能是您要在此处使用的参数。

see: 看到:
https://github.com/plataformatec/devise/blob/v4.2.0/CHANGELOG.md#420---2016-07-01 https://github.com/plataformatec/devise/blob/v4.2.0/CHANGELOG.md#420---2016-07-01
https://github.com/plataformatec/devise/blob/v4.2.0/lib/devise/parameter_sanitizer.rb#L108 https://github.com/plataformatec/devise/blob/v4.2.0/lib/devise/parameter_sanitizer.rb#L108

You may use this code for reference and change the params of your column name in user table that you want.i am using username Add this in your application_controller.rb 您可以使用此代码作为参考,并在所需的用户表中更改列名的params 。我正在使用username将其添加到application_controller.rb

  before_filter :configure_permitted_parameters, if: :devise_controller?

###### PROTECTED METHODS #######################
  protected
  def configure_permitted_parameters
    devise_parameter_sanitizer.for(:account_update) << :username
    devise_parameter_sanitizer.for(:sign_up) << :username
  end

Hope this helps :) 希望这可以帮助 :)

you have problem on signup time so permit params this way. 您在注册时间上遇到问题,因此可以通过这种方式进行参数设置。 also check Strong Parameters on device gem. 还要检查设备gem上的Strong Parameters。

before_filter :update_sanitized_params, only: [:create], if: :devise_controller?

    protected
        def update_sanitized_params
          devise_parameter_sanitizer.for(:sign_up) {|u| u.permit(:first_name, :last_name, :address, :gender, :DOB,:email, :password, :password_confirmation,:image, :thumb, :medium)}
        end

well, actually i fixed it but the thing is when i go to console do User.last and i get nill 好吧,实际上我已经修复了它,但是问题是当我去控制台做User.last时,我得到了零

class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
before_action :configure_devise_permitted_parameters, if: :devise_controller?

protected
def configure_devise_permitted_parameters
registration_params = [
   :first_name, :last_name, :address, :gender, :DOB, 
   :email, :password, :password_confirmation, 
   :image, :thumb, :medium
]

if params[:action] == 'update'
  devise_parameter_sanitizer.for(:account_update) { 
    |u| u.permit(registration_params << :current_password)
  }
elsif params[:action] == 'create'
  def configure_devise_permitted_parameters
  devise_parameter_sanitizer.fot(:sign_up).push(registration_params)
end
end
end

end 结束

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM