In this post we will cover adding a username to Clearance users in Ruby on Rails. This post assumes you already completed Clearance setup instructions for your application and generated the appropriate models, views and controllers.
Generate Username Migration
Generate a migration to add an additional column to the
users table called
$ bundle exec rails generate migration AddUsernameToUsers username:string
Open your newly generated migration file to ensure column addition is correct. In the code listing below, uniqueness and non-null validations are added to the database column configuration. These are optional, but, recommended.
class AddUsernameToUsers < ActiveRecord::Migration[6.1] def change add_column :users, :username, :string, unique: true, null: false end end
On verifying your migration appears correct run the migration via Rails command line tools to apply the schema changes:
$ bundle exec rails db:migrate
Optional: Add Regular Expression Validations to User Model
As an optional addition, you may consider adding a regular expression validation check to the User model to ensure that username are consistent and sanitized. For example:
Override the UsersController
UsersController for your application that inherits from
Clearance::UsersController in order to add additional behaviour:
new actions to create and render Users, respectively. In this child controller, will need override the strong parameters method
user_from_params which is inherited from the parent class to include the
username field form the migration. This strong parameters method is used by both the
new actions in the parent class.
The above snippet is a modified version of the
user_from_params method in the parent class of
Clearance::UsersController that includes the
Update Clearance Views to Include Username
UsersController capable of accepting usernames in the payload sent by the view, we can update the user sign up view to include a
username field during the sign up process. This assumes you are using stock views provided by Clearance via:
bundle exec rails generate clearance:views.
The primary modification is adding an addition field for the
username above the partial template provided by Clearance. On addition, the sign up form will now include a username field to complete in order to sign up for the application: