Adding a Username to the User & Admin Model

In this tutorial I will be using the following gems

  • Rails 4.0
  • Devise 3.2

If you haven’t already you should check out my previous post Creating User and Admin Model using Devise Rails 4.0. This post will build off the previous post.

We can begin by adding a username as a string into the database.

$ rails generate migration add_username_to_users username:string:uniq

Next  you have to migrate the database.

$ rake db:migrate

This will add the username into the database.

==  AddUsernameToUsers: migrating =============================================
-- add_column(:users, :username, :string)
-> 0.0012s
==  AddUsernameToUsers: migrated (0.0018s) ====================================

In order to get the username to work you need to add strong parameters to the application controller. You can find this in “app/controllers/application_controller.rb”

class ApplicationController < ActionController::Base
  before_filter :configure_permitted_parameters, if: :devise_controller?


  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(:username, :email, :password, :remember_me) }

This will whitelist all of the parameters that you list. This is a very good feature to use because we only want to allow the user to sign up and sign in using these parameters.

You can optionally add this line to whitelist the account settings.

devise_parameter_sanitizer.for(:account_update) {|u| u.permit(:username, :email, :password, :password_confirmation, :current_password)}

Next we will generate the username for the admin model.

$ rails generate migration add_username_to_admin username:string:uniq

Then we have to migrate the username to the database.

$ rake db:migrate
==  AddUsernameToAdmin: migrating =============================================
-- add_column(:admins, :username, :string)
-> 0.0015s
==  AddUsernameToAdmin: migrated (0.0019s) ====================================

The devise views must be edited in order for the username field to appear in the sign_in and sign_up forms. Add this line of code to the following files.

<div><%= f.label :username %><br />
<%= f.text_field :username %></div>

Thats it! You have successfully created a User and Admin model with a username.

I used this page to help me write this: How To: Allow users to sign in using their username or email address

Look for my next post showing you how to sign in with the username or email.


Tags: ,

2 responses to “Adding a Username to the User & Admin Model”

  1. Manuel Quintanilla says :

    Hi Daniel,

    Assuming you’ve completed:

    You may get an error when running this tutorial:

    rails generate migration add_username_to_admin username:string:uniq
    rake db:migrate

    this was putting up an error for some reason it didn’t add the |or| properly:
    error: resource_name, :url => user_registration_path | admin_registration_path) do |f| %>

    correct: resource_name, :url => user_registration_path || admin_registration_path) do |f| %>

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: