Rails: How to add custom fields to Devise

Posted by samsonyuwono on August 24, 2017

In a nutshell, Devise is a Rails engine that provides a lot of authentication needs. It has its own views, controllers, and defines it’s own routes.

The view above for Devise’s sign up page is provided by running $ rails generate devise:views users

When I started my Rails project with Devise, I initially thought that it would be a matter of inserting my custom field to the sign up views page and adjusting accordingly.

I was very wrong in that assumption and within this blog post I am going to provide a quick step by step walkthrough on how to add custom fields to your Devise pages. My intention is to provide a concise guide to this simple process.

Step 1 Go to config/initializers/devise.rb Uncomment the line below and change to “true”.

Step 2

In this step, we will create a new migration. rails generate migration add_username_to_users username:string

Fill out your generated DB file with your desired fields similar to the image below. Migrate your new file with rake db:migrate after the file has been filled out.

Note: Do not try to add your custom columns in your initial Devise migration, it will not work.

Step 3

Go to app/views/users/registration/new.html.erb or app/views/users/registration/edit.html.erb. From here you can now add your custom fields to your sign up or edit form.

Your sign up page should look something like the image below.

That’s it! You now have the ability to add custom fields to your user sign up and edit pages.