Menu

Add username to Devise

January 14, 2015 - Rails

Either follow this link

https://github.com/plataformatec/devise/wiki/How-To:-Allow-users-to-sign-in-using-their-username-or-email-address

follow the steps

step -1- rails g migration add_field_to_modal username:string

step -2 – rake db:migrate

step -3 – add attr_accessible :username

step -4 – in application.rb

before_filter :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
step -5 – in config/initializersif you want to replace email by usernname

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

Leave a Reply