How to add username field to devise gem?

devise, ruby-on-rails, ruby-on-rails-4

Solution

Answer is now outdated [ Valid for rails4 ]

I have done the same. Please follow these steps:

`rails generate migration add_username_to_users username:string:uniq`

`rake db:migrate`

add `attr_accessible :username`

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 

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 ]

update the views.

Note if `attr_accessible :username`gives error try `attr_accessor :username`

Problem

Here is what I tried, `rails g migration add_username_to_hrs` `bundle exec rake db:migrate` added the `attr_accessible:username` restarted the server My add_username_to_hr.rb ``` class AddUsernameToAuthorize < ActiveRecord::Migration def change add_column :authorizes, :username, :string end end ``` Error undefined method `username' for # Question: How can I add a username field in my devise gem?

Original source