Adding extra registration fields with Devise

前端 未结 8 640
情话喂你
情话喂你 2020-12-01 06:57

I am trying to add some extra fields to registrations#new. Since I only want extra data and do not need different functionality, I don\'t see why I need to override controll

8条回答
  •  自闭症患者
    2020-12-01 07:12

    It would appear that the code sample in your question is not working because you are not setting the before_filter to call the sanitizer.

    before_filter :configure_permitted_parameters, if: :devise_controller?
    

    With that said, it's probably better to override the controller, as shown in the accepted answer, so that the application controller isn't doing this check all of the time. The accepted answer can be shortened up with the code below. I've tested this code with my application and it works well. All of this is documented in the Strong Parameters section of the README in the 3.0.0.rc tag.

    Override the controller:

    class RegistrationsController < Devise::RegistrationsController
      before_filter :configure_permitted_parameters, :only => [:create]
    
      protected
    
        def configure_permitted_parameters
          devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:username, :email, :password) }
        end
    end
    

    Then update the routes to use it:

    devise_for :members, :controllers => { :registrations => "registrations" }
    

提交回复
热议问题