skip to content

With Devise 5, how do you let the sign-up form save extra fields such as company_name, and where can that code live?

level: middleimportance: should knowfreq 58%

answer

  1. unpermitted keys silently dropped
  2. devise_parameter_sanitizer.permit(:sign_up, keys:)
  3. before_action ..., if: :devise_controller?
  4. keys appends, except removes, block replaces
  5. devise:controllers plus controllers: in devise_for

basics

~10 s

Devise builds the user from devise_parameter_sanitizer.sanitize(:sign_up), which permits only the authentication keys, password and password_confirmation. Add fields with devise_parameter_sanitizer.permit(:sign_up, keys: [:company_name]) in a filter or in an overridden registrations controller.

solid answer

~30 s

`Devise::RegistrationsController#create` calls `build_resource(sign_up_params)`, and `sign_up_params` is `devise_parameter_sanitizer.sanitize(:sign_up)`. By default that permits the authentication keys (email) plus `password` and `password_confirmation`; `:account_update` adds `current_password`, `:sign_in` adds `remember_me`. Anything else is dropped without an error. Either add a `before_action :configure_permitted_parameters, if: :devise_controller?` in `ApplicationController` that calls `devise_parameter_sanitizer.permit(:sign_up, keys: [:company_name])`, or generate `Users::RegistrationsController` with `rails generate devise:controllers users -c registrations`, point `devise_for :users, controllers: { registrations: "users/registrations" }` at it and permit there. `keys:` appends, `except:` removes, and a block replaces the list with your own `permit` call.

code

ruby · 10 lines
ruby
class ApplicationController < ActionController::Base
  before_action :configure_permitted_parameters, if: :devise_controller?

  protected

  def configure_permitted_parameters
    devise_parameter_sanitizer.permit(:sign_up, keys: [:company_name])
    devise_parameter_sanitizer.permit(:account_update, keys: [:company_name])
  end
end

go deeper

for a junior

Recall that extra sign-up fields must be permitted with devise_parameter_sanitizer.permit for :sign_up and :account_update.

for a middle

Explain the three actions and their defaults, keys versus except versus the block form, and the controller-override route through devise_for.

for a senior

Choose between a global filter and an overridden controller, extend actions with super and a block, and keep privilege fields out of sign-up.

for a principal

Judge how far to customise Devise's controllers before the overrides cost more than owning the registration flow outright.

## Why the extra field disappears A new sign-up form in a **tenant portal** collects `email`, `password` and the tenant's `company_name`. The user is created, but `company_name` is blank and nothing raised. The reason is **strong parameters** applied by Devise itself: - `Devise::RegistrationsController#create` runs `build_resource(sign_up_params)`. - `sign_up_params` is `devise_parameter_sanitizer.sanitize(:sign_up)`. - The **parameter sanitizer** (`Devise::ParameterSanitizer`) permits a fixed list per action and drops everything else. Its defaults, always combined with the model's `authentication_keys` (`[:email]` unless configured): | Action | Permitted by default | |---|---| | `:sign_in` | auth keys, `password`, `remember_me` | | `:sign_up` | auth keys, `password`, `password_confirmation` | | `:account_update` | auth keys, `password`, `password_confirmation`, `current_password` | Calling `sanitize` with any other action raises `NotImplementedError` until you `permit` it first. ## The `permit` API `devise_parameter_sanitizer.permit(action, keys: nil, except: nil, &block)` changes one action's list: - `keys:` **appends** to the existing list, which already includes the auth keys and defaults; - `except:` **removes** keys, for example `permit(:account_update, except: [:password])`; - a **block** replaces the list entirely: it receives the resource's `ActionController::Parameters` and you call `permit` yourself, which is how you allow arrays or nested hashes. ## Option 1: a filter in `ApplicationController` The most common place is a filter that runs only for Devise controllers: 1. `before_action :configure_permitted_parameters, if: :devise_controller?` 2. a protected `configure_permitted_parameters` that calls `permit(:sign_up, keys: [:company_name])` and `permit(:account_update, keys: [:company_name])`. It is short and covers every Devise scope, but it puts registration concerns into the base controller of the whole app. ## Option 2: override the controller When registration needs more than extra keys (a custom redirect, creating a tenant record, a different layout), override the controller: 1. `rails generate devise:controllers users -c registrations` writes `app/controllers/users/registrations_controller.rb` with `class Users::RegistrationsController < Devise::RegistrationsController` and commented hooks. 2. Route to it: `devise_for :users, controllers: { registrations: "users/registrations" }`. **Without this line Devise keeps using its own controller** and the new file is never called. 3. Uncomment `before_action :configure_sign_up_params, only: [:create]` and permit there. 4. Override actions by calling `super` with a block: Devise's actions `yield resource`, so `super { |user| ... }` runs your code after the save attempt without copying the action. 5. Override path hooks such as `after_sign_up_path_for` and `after_inactive_sign_up_path_for` (the latter is used when `confirmable` leaves the new user inactive). To change the form markup, copy the views with `rails generate devise:views` (or `devise:views users` with `config.scoped_views = true` for per-scope views; `-v registrations` limits which folders are copied). ## Account updates and `current_password` The edit-account form goes through the same machinery with the `:account_update` list. `Devise::RegistrationsController#update` calls `update_resource`, which calls the model's `update_with_password`: - it removes `current_password` from the params and checks it with `valid_password?`; - if `password` is blank it is dropped, so a user can change other fields without choosing a new password; - on a wrong or missing `current_password` it adds an error to that field and returns `false`. To let users edit profile fields without retyping their password, override `update_resource` in your registrations controller to call `update_without_password`, which refuses password changes; keep sensitive fields such as `email` behind the password check. ## Signing in by a username Permitting `username` on `:sign_up` stores it; signing in with it is a separate change: set `config.authentication_keys` to include it, and the sanitizer then permits it for every action automatically because auth keys are always included. ## Mistakes to avoid - Calling `params.permit` in a custom action and bypassing the sanitizer, so `:account_update` and `:sign_up` drift apart. - Forgetting `:account_update`, so the field can be set at sign-up but never edited. - Permitting role or tenant-ownership fields on `:sign_up`, which lets a visitor choose their own privileges.

  • How do you permit an array or a nested hash, such as notification_channels: [] , on sign-up?
    Use the block form: `devise_parameter_sanitizer.permit(:sign_up) { |user| user.permit(:email, :password, :password_confirmation, notification_channels: []) }`. The block replaces the list, so you must name the defaults again; `keys:` cannot express array or hash shapes.
  • Why prefer super with a block over copying Devise's create action into your controller?
    Devise's actions `yield resource` at the useful point, so `super { |user| ... }` adds behaviour while keeping Devise's flash messages, sign-in, confirmation branch and responder statuses. A copied action freezes today's implementation and silently misses fixes in later Devise releases.

  • Devise permits every column of the users table on sign-up
  • keys: replaces the default list, so password must be repeated
  • Generating Users::RegistrationsController is enough without changing devise_for
  • An unpermitted sign-up field raises an error that shows up in the logs
  • Permitting a role field on sign_up is harmless because the form does not show it