From ed625a804ff7a374f44a708747342d72104ee67c Mon Sep 17 00:00:00 2001 From: Taketo Takashima Date: Fri, 12 Sep 2025 14:54:00 +0900 Subject: [PATCH 1/2] Use Devise.responder.error_status instead of fixed :unprocessable_entity in confirmations and unlocks controllers --- app/controllers/devise/confirmations_controller.rb | 4 ++-- app/controllers/devise/unlocks_controller.rb | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/app/controllers/devise/confirmations_controller.rb b/app/controllers/devise/confirmations_controller.rb index 5e22079ec8..c2cbd0c254 100644 --- a/app/controllers/devise/confirmations_controller.rb +++ b/app/controllers/devise/confirmations_controller.rb @@ -27,8 +27,8 @@ def show set_flash_message!(:notice, :confirmed) respond_with_navigational(resource){ redirect_to after_confirmation_path_for(resource_name, resource) } else - # TODO: use `error_status` when the default changes to `:unprocessable_entity`. - respond_with_navigational(resource.errors, status: :unprocessable_entity){ render :new } + # Use `error_status` when the default changes to `:unprocessable_entity`. + respond_with_navigational(resource.errors, status: (Devise.responder.error_status != :ok) ? Devise.responder.error_status : :unprocessable_entity ){ render :new } end end diff --git a/app/controllers/devise/unlocks_controller.rb b/app/controllers/devise/unlocks_controller.rb index b1487760b6..00cccce613 100644 --- a/app/controllers/devise/unlocks_controller.rb +++ b/app/controllers/devise/unlocks_controller.rb @@ -29,8 +29,8 @@ def show set_flash_message! :notice, :unlocked respond_with_navigational(resource){ redirect_to after_unlock_path_for(resource) } else - # TODO: use `error_status` when the default changes to `:unprocessable_entity`. - respond_with_navigational(resource.errors, status: :unprocessable_entity){ render :new } + # Use `error_status` when the default changes to `:unprocessable_entity`. + respond_with_navigational(resource.errors, status: (Devise.responder.error_status != :ok) ? Devise.responder.error_status : :unprocessable_entity ){ render :new } end end From e7f55961f2c9f7e97482f5d59f65358a8ec336c4 Mon Sep 17 00:00:00 2001 From: Taketo Takashima Date: Fri, 12 Sep 2025 15:44:19 +0900 Subject: [PATCH 2/2] Use `:unprocessable_content` in generated Devise config for Rack 3.1+ For rack 3.1 and higher, devise config uses `:unprocessable_content` instead of `:unprocessable_entity`. For rack 3.0 and below, it continues to use `:unprocessable_entity`. --- README.md | 3 ++- app/controllers/devise/confirmations_controller.rb | 4 ++-- app/controllers/devise/unlocks_controller.rb | 4 ++-- lib/generators/templates/devise.rb | 2 +- 4 files changed, 7 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 538bb2a77b..dd176cd8fe 100644 --- a/README.md +++ b/README.md @@ -484,7 +484,8 @@ Devise.setup do |config| # apps is `200 OK` and `302 Found` respectively, but new apps are generated with # these new defaults that match Hotwire/Turbo behavior. # Note: These might become the new default in future versions of Devise. - config.responder.error_status = :unprocessable_entity + config.responder.error_status = :unprocessable_content # for Rack 3.1 or higher + # config.responder.error_status = :unprocessable_entity # for Rack 3.0 or lower config.responder.redirect_status = :see_other end ``` diff --git a/app/controllers/devise/confirmations_controller.rb b/app/controllers/devise/confirmations_controller.rb index c2cbd0c254..258dc179b3 100644 --- a/app/controllers/devise/confirmations_controller.rb +++ b/app/controllers/devise/confirmations_controller.rb @@ -27,8 +27,8 @@ def show set_flash_message!(:notice, :confirmed) respond_with_navigational(resource){ redirect_to after_confirmation_path_for(resource_name, resource) } else - # Use `error_status` when the default changes to `:unprocessable_entity`. - respond_with_navigational(resource.errors, status: (Devise.responder.error_status != :ok) ? Devise.responder.error_status : :unprocessable_entity ){ render :new } + # Use `error_status` when the default changes to `:unprocessable_content` (or `:unprocessable_entity`). + respond_with_navigational(resource.errors, status: (Devise.responder.error_status != :ok) ? Devise.responder.error_status : Rack::Utils::SYMBOL_TO_STATUS_CODE.key(422) ){ render :new } end end diff --git a/app/controllers/devise/unlocks_controller.rb b/app/controllers/devise/unlocks_controller.rb index 00cccce613..43b014476c 100644 --- a/app/controllers/devise/unlocks_controller.rb +++ b/app/controllers/devise/unlocks_controller.rb @@ -29,8 +29,8 @@ def show set_flash_message! :notice, :unlocked respond_with_navigational(resource){ redirect_to after_unlock_path_for(resource) } else - # Use `error_status` when the default changes to `:unprocessable_entity`. - respond_with_navigational(resource.errors, status: (Devise.responder.error_status != :ok) ? Devise.responder.error_status : :unprocessable_entity ){ render :new } + # Use `error_status` when the default changes to `:unprocessable_content` (or `:unprocessable_entity`). + respond_with_navigational(resource.errors, status: (Devise.responder.error_status != :ok) ? Devise.responder.error_status : Rack::Utils::SYMBOL_TO_STATUS_CODE.key(422) ){ render :new } end end diff --git a/lib/generators/templates/devise.rb b/lib/generators/templates/devise.rb index 9e6744bd7d..cefe7747ad 100644 --- a/lib/generators/templates/devise.rb +++ b/lib/generators/templates/devise.rb @@ -302,7 +302,7 @@ # apps is `200 OK` and `302 Found` respectively, but new apps are generated with # these new defaults that match Hotwire/Turbo behavior. # Note: These might become the new default in future versions of Devise. - config.responder.error_status = :unprocessable_entity + config.responder.error_status = <%= Rack::Utils::SYMBOL_TO_STATUS_CODE.key(422).inspect %> config.responder.redirect_status = :see_other # ==> Configuration for :registerable