Skip to content

Instantly share code, notes, and snippets.

@withoutwax
Last active April 27, 2024 00:43
Show Gist options
  • Save withoutwax/46a05861aa4750384df971b641170407 to your computer and use it in GitHub Desktop.
Save withoutwax/46a05861aa4750384df971b641170407 to your computer and use it in GitHub Desktop.
Adding Custom Fields to Devise

Adding Custom Fields to Devise

Before doing anything, please check the versions of the gem files:

gem 'rails', '~> 5.1.4'
gem 'devise', '~> 4.4.0'

Disclaimer - This solution may not work with older versions of rails and devise. Versions listed above were the latest versions of gems which I was using at the time of creation and tests. (10 Jan 2018)

Step 01: Install & Generate Devise

If you do not know the install process, please visit Getting Started part of Devise README.md

Step 02: Change code in devise.rb

In order to visually see all the new fields and alterations of login, sign up etc pages, we need to first change the following code in: config/initializers/devise.rb

config.scoped_views = true

Step 03: Generate a migration file for model created with Devise

bash

$ rails generate migration add_name_to_users name:string

Step 04: Check Migration file

_db/migrate/....add_name_to_users.rb

class AddNameToUsers < ActiveRecord: :Migration
     def change
          add_column :users, :name, :string
     end
end

Step 05: Migrate Database

bash

$ rake: db:migrate

Step 06: Update code in ApplicationController

Similar to permitting parameters, we need to create permitting parameters to devise model as well.
app/controllers/application_controller.rb

class ApplicationController < ActionController: :Base
     protect_from_forgery with: :exception

     before_action :configure_permitted_parameters, if: :devise_controller?

     protected

          def configure_permitted_parameters
               devise_parameter_sanitizer.permit(:sign_up) { |u| u.permit(:name, :email, :password)}

               devise_parameter_sanitizer.permit(:account_update) { |u| u.permit(:name, :email, :password, :current_password)}
          end

end

Step 07: Register & Edit Users with custom fields

Add new custom fields inside html.erb files. app/views/layout/index.html.erb

<%= link_to 'Edit Profile', edit_user_registration_path %>

app/views/users(or devise)/registrations/new.html.erb

<div>
     <%= f.label :name %><br />
     <%= f.text_field :name, autofocus: true %>
</div>

app/views/users(or devise)/registrations/edit.html.erb

<div>
     <%= f.label :name %><br />
     <%= f.text_field :name, autofocus: true %>
</div>
@samslow
Copy link

samslow commented Jan 18, 2019

Step 06: Update code in ApplicationController part is missing one end
please insert end to above last end

@amarduwal
Copy link

I believe it won't be hard to figure out for anyone. :)

@benkoshy
Copy link

benkoshy commented Feb 11, 2020

# It might be worth adding: prepend: true to the below line:

protect_from_forgery with: :exception, prepend: true

## Adding the prepend: true makes explicitly clear that the protect_from_forgery before action is executed first, before all others.
## I think that was added in rails 5. Otherwise if you stick in another before action before protect_from_forgery then that action
## might be executed first, which might not be what you want: Caveat emptor!

@guillemdlopez
Copy link

Thank you, very useful!

@thisismydesign
Copy link

thisismydesign commented Jan 22, 2021

This doesn't seem to work for me. Did it as outlined here and in the docs but the user is not updated in my specs.

before { sign_in(user) }
subject(:send_request) { patch :update, params: { user: {name: name} } }

This returns 200 but on user.reload the user is still the name unchanged.. Any idea why?

Edit: found a Wiki entry about this topic: https://github.com/heartcombo/devise/wiki/How-To:-Allow-users-to-edit-their-account-without-providing-a-password

@rztprog
Copy link

rztprog commented Aug 30, 2021

All work for me, thanks !!!

@Lucashustler
Copy link

Works great. Thank you :)

@UzairFahima3
Copy link

Replace ActionController: :Base with ActionController::Base

@Relderf
Copy link

Relderf commented Nov 25, 2022

Very useful!

@Eedrisserh
Copy link

I followed the same process to add first_name, and last_name fields making necessary addition where needed. It worked like a magic. Thanks for the guide.

@benkoshy
Copy link

@mjsorribas Can you confirm whether migrations exists to back those fields in the User.rb model? If you have any further questions or doubts, why not open a stack overflow question?

@flstudio4
Copy link

You are a life saver, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment