Adding name attribute to `User` in Devise

后端 未结 8 1635
轻奢々
轻奢々 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 01:17

    Add this code to application_controller.rb

    before_action :configure_permitted_parameters, if: :devise_controller?
    
    
    
    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
    
    0 讨论(0)
  • 2020-12-31 01:18

    For Rails 5 (in fact devise 4)

    Tested for: rails 5.1.0 (devise 4.2.1)

    There is no need to work with devise controllers.

    Just add the following to your application_controller.rb:

    before_action :configure_permitted_parameters, if:  :devise_controller?
    
    
    protected
    
      def configure_permitted_parameters
    
        devise_parameter_sanitizer.permit(:sign_up, keys: [:name])
    
      end
    

    devise_parameter_sanitizer.for no longer works with Rails 5 (to be more correct, it is not supported in devise 4, which is the expected devise version in a Rails 5 context): use devise_parameter_sanitizer.permit to avoid undefined method 'for' for #<Devise::ParameterSanitizer error

    0 讨论(0)
提交回复
热议问题