添加自定义参数以设计注册 - 未允许的参数

Bog*_*iel 11 ruby-on-rails devise ruby-on-rails-3 ruby-on-rails-5

我一直在尝试自定义设计寄存器方法以注册更多参数并更新更多(到目前为止没有运气),但我总是得到Unpermitted parameters:错误.我尝试使用Devisehttps://github.com/plataformatec/devise#strong-parameters 添加额外的注册字段,但我无法克服这一点.

我还考虑过创建一个新表来保存用户id的外键并放入类似的东西user_id, display_name, profile_picture,但是在尝试从同一页面提交所有内容时我会遇到同样的问题(与设计控制器混乱).

您对我如何解决这个问题有什么建议吗?我还有什么要发布的?

的routes.rb

devise_for :users, controllers: { registrations: 'users/registrations' }
Run Code Online (Sandbox Code Playgroud)

用户/ REGC

def create
    build_resource(registration_params)

    if resource.save
      if resource.active_for_authentication?
        set_flash_message :notice, :signed_up if is_navigational_format?
        sign_up(resource_name, resource)
        respond_with resource, :location => after_sign_up_path_for(resource)
      else
        set_flash_message :notice, :"signed_up_but_#{resource.inactive_message}" if is_navigational_format?
        respond_with resource, :location => after_sign_up_path_for(resource)
      end
    else
      clean_up_passwords
      respond_with resource
    end
  end

private
  def registration_paramss
    params.require(:user).permit(:email, :display_name, :terms_of_services, :profile, :password, :password_confirmation)
  end
Run Code Online (Sandbox Code Playgroud)

gka*_*ats 24

看起来你只需要告诉设计应该允许哪些参数.默认情况下,设计允许电子邮件(或用户名取决于配置),密码和password_confirmation参数.你只需要添加更多.

色器件文档建议设置此功能的"懒办法".

class ApplicationController < ActionController::Base
  before_action :configure_permitted_parameters, if: :devise_controller?

  protected

  def configure_permitted_parameters
    devise_parameter_sanitizer.permit(:sign_up, keys: [:display_name])
  end
end
Run Code Online (Sandbox Code Playgroud)

然后文档说明了这一点

class Users::RegistrationsController < Devise::RegistrationsController
  def create
    # Your custom code here. Make sure you copy devise's functionality
  end

  private

  # Notice the name of the method
  def sign_up_params
    params.require(:user).permit(:display_name, :email, :password, :password_confirmation)
  end
end
Run Code Online (Sandbox Code Playgroud)

只有当您需要覆盖accepts_nested_attributes_for操作时,才应提供自定义路径.在这种情况下,请确保您也覆盖该registrations#create方法.

class ApplicationController < ActionController::Base
  before_action :configure_permitted_parameters, if: :devise_controller?

  protected

  def configure_permitted_parameters
    devise_parameter_sanitizer.permit(:sign_up, keys: [:display_name])
  end
end
Run Code Online (Sandbox Code Playgroud)

实质上,您必须查看您的注册表单如何发布参数以了解如何在控制器中配置强参数.请务必阅读强参数语法.

希望能帮助到你!


Jbu*_*r43 9

对于Devise 4.2.0,您可以通过将这些值添加到密钥来为用户表列出其他参数.默认情况下,设计会为您提供评论.下面我补充道:avatar

  # If you have extra params to permit, append them to the sanitizer.
  def configure_sign_up_params
    devise_parameter_sanitizer.permit(:sign_up, keys: [:attribute, :avatar])
  end
Run Code Online (Sandbox Code Playgroud)


Cod*_*ast 5

接受的答案说配置应该放在你的 applicationController 中,但它可以简单地放在你的用户注册控制器中,你可以指定你只想为 create 方法运行它,而不是别的:


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: [:enter_param_name_here])
  end
end
Run Code Online (Sandbox Code Playgroud)