简体   繁体   English

rails - 设计强参数总是不允许注册

[英]rails - devise strong parameters is always unpermitted for registration

I want to permit :full_name parameter for my user model registration in devise, and I always getting Unpermitted parameter: :full_name as response for Users::RegistrationsController#create action我想在设计中允许:full_name参数用于我的用户模型注册,并且我总是得到Unpermitted 参数: :full_name作为对 Users::RegistrationsController#create 操作的响应

I have tried several ways as I show you next:在接下来向您展示时,我尝试了几种方法:

1. Application controller 1. 应用控制器

class ApplicationController < ActionController::Base
  protect_from_forgery with: :exception
  before_action :configure_permitted_parameters, if: :devise_controller?
  
  protected
  
  def configure_permitted_parameters
    case params[:action]
    when 'create'
        devise_parameter_sanitizer.permit(:sign_up, keys: %i[full_name email password password_confirmation])
    when 'update'
        ...
    end
  end
end

Result => Unpermitted parameter: :full_name结果 => 不允许的参数::full_name

2. Registration controller 2.注册控制器

class Users::RegistrationsController < Devise::RegistrationsController
  before_action :configure_sign_up_params, only: :create
  
  protected
  
  def configure_sign_up_params
    params.require(:user).permit(%i[full_name email password password_confirmation])
  end
end

Result => Unpermitted parameter: :full_name结果 => 不允许的参数::full_name

3. Registration controller 3.注册控制器

class Users::RegistrationsController < Devise::RegistrationsController
  before_action :configure_sign_up_params, only: :create
  
  protected
  
  def configure_sign_up_params
    devise_parameter_sanitizer.permit(:sign_up, keys: %i[full_name email password password_confirmation])
  end
end

Result => Unpermitted parameter: :full_name结果 => 不允许的参数::full_name

In my gemfile:在我的 gemfile 中:

gem 'devise', '~> 4.8'

In my routes:在我的路线中:

devise_controllers = {
  confirmations: 'users/confirmations',
  registrations: 'users/registrations',
  invitations: 'users/invitations',
}
devise_for :users, controllers: devise_controllers

I have read devise strong params but to be honest I do not know what I am doing wrong.我读过设计强大的参数,但老实说我不知道​​我做错了什么。

Also I tried to debug in Users::RegistrationsController#create what is happening with:我还尝试在 Users::RegistrationsController#create 中调试发生了什么:

def create
  super do
    binding.pry
  end
end

but it skips the debugger breakpoint... do you have any idea what is going on?但它跳过调试器断点...你知道发生了什么吗?

Cheers!干杯!

Generally, you write strong params for a specific controller, not in your ApplicationController, because the permitted conditions will be different for each model.通常,您为特定控制器编写强参数,而不是在 ApplicationController 中,因为每个模型允许的条件不同。 When using devise_parameter_sanitizer , you only need to include the extra fields you're adding - this isn't setting up your strong params from scratch, just adding keys to the default Devise list.使用devise_parameter_sanitizer ,您只需要包含您添加的额外字段 - 这不是从头开始设置您的强参数,只需将键添加到默认设计列表。

So, you should find that this is all you need in your Users::RegistrationsController.因此,您应该会发现这就是您在 Users::RegistrationsController 中所需的全部内容。

def configure_sign_up_params
  devise_parameter_sanitizer.permit(:sign_up, keys: [:full_name])
end

(BTW, ensure you refer to the parameter correctly, as params[:user][:full_name] .) (顺便说一句,请确保您正确引用参数,如params[:user][:full_name] 。)

(Oh, and if you want to do debugging, I'd suggest installing the byebug gem. You just add an extra line byebug where you want to have a breakpoint.) (哦,如果您想进行调试,我建议安装 byebug gem。您只需在要设置断点的地方添加额外的一行byebug 。)

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

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