How to add a username field to create a gem?

Here is what I tried

  • rails g migration add_username_to_hrs

  • bundle exec rake db:migrate

  • added 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 

Mistake

undefined username method for #

Question How to add a username field to my gem?

+7
ruby-on-rails ruby-on-rails-4 devise
source share
6 answers

I did the same. 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 with usernname

     config.authentication_keys = [ :username ] config.case_insensitive_keys = [ :username ] config.strip_whitespace_keys = [ :username ] 
  • refresh views.

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

+7
source share

There is a devile wiki guide allowing users to log in with a username, maybe this can help you?

+7
source share

If you use rails 4, enter the code below into the 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 
+5
source share

If you are using rails 4, follow these steps:

  • rails g migration AddUserNameToAuthorize

  • rake db:migrate

  • put this code in application_controller.rb to accept the 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 
+4
source share

Consider whether you really add the field to the model that you specified during installation. If you added migration to this model. For example, you used the program for the user model, then you can create a migration to add the username to the user model and run db: migrate and add attr_accessible: the username for the model if you use rails <4

+2
source share

You need to add the username to the model that you specified in the following command

 rails g devise <modelname> 

Once you're done, you need to follow the steps above, but you need to change the configuration file to look for a username for login instead of email. Then restart the rails server and everything should be fine.

+2
source share

All Articles