Frontend Developers: I'm redesigning DevTools. Want early access?

How to add fields to the Devise registration form

This post will show you how to add custom fields to your Devise registration form in Rails 5. For this example, we’ll add a name field.

1. Make sure you’ve added Devise, generated its views, and migrated your custom fields.

# generate Devise views
rails generate devise:views

# add custom fields
rails g migration AddNameToUsers name:string
db:migrate

2. Add a new controller at app/controllers/registrations_controller.rb. Make sure it inherits from Devise::RegistrationsController, and include your form fields in sign_up_params.

class RegistrationsController < Devise::RegistrationsController

  private

    def sign_up_params
      params.require(:user).permit(:email, :password, :name)
    end
end

3. Let’s tell Devise to use this controller. In config/routes.rb:

devise_for :users, :controllers => {
  registrations: 'registrations'
}

4. Add your field to app/views/devise/registrations/new.html.erb.

<div class="field">
	<%= f.label :name %>
  <%= f.text_field :name %>
</div>

That’s all there is to it. You’ve added a custom field to your Devise registration form. And if you want to allow the user to edit this value too, you can add an account_update_params method to your Registrations Controller and customize app/views/devise/registrations/edit.html.erb.

Frontend Developers: I'm redesigning DevTools. Want early access?