Saving custom fields in devise User model in rails 4

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

    In Rails 4.2, this is how I did. I have User Model on which devise is applied.
    Use this command "rails generate devise:controllers users" to generate custom controllers.
    I have added "username" name attribute to my User Model

    In my controller

    class Users::RegistrationsController < Devise::RegistrationsController
     before_filter :configure_sign_up_params, only: [:create]
     before_filter :configure_account_update_params, only: [:update]
     #rest of code as generated
    
       protected
    
        # If you have extra params to permit, append them to the sanitizer.
        def configure_sign_up_params
          devise_parameter_sanitizer.for(:sign_up) << :username
        end
    
       # If you have extra params to permit, append them to the sanitizer.
       def configure_account_update_params
         devise_parameter_sanitizer.for(:account_update) << :username
       end
    

    In Routes

    devise_for :users, controllers: {registrations: "users/registrations"}
    

提交回复
热议问题