Saving custom fields in devise User model in rails 4

前端 未结 4 1034
名媛妹妹
名媛妹妹 2021-02-05 20:24

I made a devise User model and added additional fields to it. When I create and account everything works fine, only with email, pw and pw conf.

I then want to allow the

4条回答
  •  南方客
    南方客 (楼主)
    2021-02-05 21:05

    After working on something similar to this, I settled on using Application Controller, then afterward found that the Devise Documentation is fairly straightforward for this in their strong parameters section and gives an alternative to using Application Controller. https://github.com/plataformatec/devise#strong-parameters

    Below is the approach with Application Controller which worked for me.

        class ApplicationController < ActionController::Base
    
          before_filter :configure_permitted_parameters, if: :devise_controller?
    
          private
    
          def configure_permitted_parameters
             devise_parameter_sanitizer.for(:sign_up){ |u| u.permit(:name, :username, :about,  :email, :password, :password_confirmation)}        
             devise_parameter_sanitizer.for(:account_update){ |u| u.permit(:name, :username, :about, :email, :password, :password_confirmation) }          
          end
    
        end
    

    This should work the same and it directly overwrites methods in Devise::RegistrationController.

        class Users::RegistrationsController < Devise::RegistrationsController
    
          private
    
          def configure_sign_up_params
            devise_parameter_sanitizer.for(:sign_up){ |u| u.permit(:name, :username, :about,  :email, :password, :password_confirmation)}
          end
    
          def configure_account_update_params
            devise_parameter_sanitizer.for(:account_update){ |u| u.permit(:name, :username, :about, :email, :password, :password_confirmation) }
          end
    
        end
    

提交回复
热议问题