Adding name attribute to `User` in Devise

后端 未结 8 1634
轻奢々
轻奢々 2020-12-31 00:31

I\'m trying to add a name attribute to the User model provided by Devise. I added a \"name\" column to my database, and changed the sign up view so that it asks for the use

相关标签:
8条回答
  • 2020-12-31 00:58

    For Rails 4

    Use like this

    protected
    def configure_permitted_parameters
      devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:username, :email, :password, :password_confirmation, :remember_me) }
      devise_parameter_sanitizer.for(:sign_in) { |u| u.permit(:login, :username, :email, :password, :remember_me) }
      devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:username, :email, :password, :password_confirmation, :current_password) }
    end
    

    Add additional fields at the end.

    0 讨论(0)
  • 2020-12-31 01:09

    uncomment some of the generated code:

    class Users::RegistrationsController < Devise::RegistrationsController
      before_filter :configure_sign_up_params, only: [:create]
    
      protected
    
      def configure_sign_up_params
        devise_parameter_sanitizer.for(:sign_up) << :name
      end
    end
    
    0 讨论(0)
  • 2020-12-31 01:12
     private
    
       def sign_up_params
         params.require(:user).permit(:name,:email,:gender,:age,:password,:password_confirmation) if params[:user].present?
       end
    

    Add this in the controller that extends devise's default registration controller

    0 讨论(0)
  • 2020-12-31 01:15

    Yes. Add :name to attr_accessible in User Model

    0 讨论(0)
  • 2020-12-31 01:16

    in your user model locate;

     attr_accessible :email, :password, :password_confirmation, :remember_me
    

    and add :name on the end

    0 讨论(0)
  • 2020-12-31 01:16

    Write this code inside the ApplicationController class...

    before_action :configure_permitted_parameters, if:  :devise_controller?
    
          protected
    
          def configure_permitted_parameters
            devise_parameter_sanitizer.for(:sign_up) << :name  
    
    0 讨论(0)
提交回复
热议问题