Saving custom fields in devise User model in rails 4

前端 未结 4 986
名媛妹妹
名媛妹妹 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:16

    First produce new field. for reference http://guides.rubyonrails.org/migrations.html

    Do you have add your new fields in user controller parameter?

       def user_params
          params.require(:user).permit(:email, :password, :password_confirmation)
        end
    
        def sign_up_params
          params.require(:user).permit(:email, :password, :password_confirmation)
        end
    

    In the application controller

    before_filter :configure_permitted_parameters, if: :devise_controller?
    
      protected
    
      def configure_permitted_parameters
        devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:email, :password, :password_confirmation)}
    
        devise_parameter_sanitizer.for(:sign_in) { |u| u.permit(:email, :password, :password_confirmation)}
      end
    

    In your registration form that override devise add this

    class Users::RegistrationsController < Devise::RegistrationsController
         skip_before_filter :verify_authenticity_token, :only => [:ipn_notification]
      def sign_up_params
          params.require(:user).permit(:email, :password, :password_confirmation)
      end
    

    After that add your new fields in all views _form,show,edit,index.

提交回复
热议问题