How to add username field to devise gem?

前端 未结 5 1289
时光说笑
时光说笑 2020-12-14 23:40

Here is what I tried,

  1. rails g migration add_username_to_hrs

  2. bundle exec rake db:migrate

  3. added the

相关标签:
5条回答
  • 2020-12-15 00:20

    If you are using rails 4 then follow this steps:

    1. rails g migration AddUserNameToAuthorize

    2. rake db:migrate

    3. put this code in application_controller.rb to accept username parameter for sign_in, sign_up and also for account_update:

      class ApplicationController < ActionController::Base
          protect_from_forgery with: :exception
          def configure_permitted_parameters
            devise_parameter_sanitizer.for(:sign_in) { |u| u.permit(:email, :password,:username) }
            devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:email, :password, :password_confirmation,:username) }
            devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:email, :password, :password_confirmation,:username) }
          end
      end
      
    0 讨论(0)
  • 2020-12-15 00:20

    You have to add username in the model which you had given in below command

    rails g devise <modelname>
    

    Once you are done then you need to follow your above steps but need to modify devise configuration file to look for username for login instead of email. Then restart rails server and it should be fine.

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

    Devise actually add the field to model which you specified during devise setup. If You have add migration to that model. For example, you have used devise for user model, then you can generate migration for adding the username to user model and run the db:migrate and add the attr_accessible :username to model, if you are using rails < 4

    0 讨论(0)
  • 2020-12-15 00:36

    Answer is now outdated [ Valid for rails4 ]

    I have done the same. Please follow these steps:

    1. rails generate migration add_username_to_users username:string:uniq

    2. rake db:migrate

    3. add attr_accessible :username

    4. in 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 
      
    5. in config/initializers if you want to replace email by usernname

      config.authentication_keys = [ :username ]
      config.case_insensitive_keys = [ :username ]  
      config.strip_whitespace_keys = [ :username ]
      
    6. update the views.

    Note if attr_accessible :usernamegives error try attr_accessor :username

    0 讨论(0)
  • 2020-12-15 00:38

    If you are using rails 4 then put below code in application controller

    class ApplicationController < ActionController::Base
     before_action :configure_permitted_parameters, if: :devise_controller?
    
     protected
    
     def configure_permitted_parameters
      devise_parameter_sanitizer.for(:sign_up) << :username
     end
    end
    
    0 讨论(0)
提交回复
热议问题