skip to content

After upgrading an older app to Devise 5 on Rails 8.1 with Turbo, failed sign-ins show no errors; which Devise settings and code must change?

level: seniorimportance: should knowfreq 46%

answer

  1. library responder: 200 and 302
  2. generated initializer: 422 and 303
  3. responder.error_status and redirect_status
  4. sign_out_via :delete without rails-ujs
  5. devise_error_messages! and Devise::TestHelpers removed

basics

~10 s

Devise's own responder defaults are 200 for errors and 302 for redirects; only newly generated initializers set 422 and 303. Add config.responder.error_status = :unprocessable_content and redirect_status = :see_other, then replace APIs Devise 5 removed.

solid answer

~40 s

`Devise::Controllers::Responder` defaults to `error_status = :ok` and `redirect_status = :found`. A fresh `rails generate devise:install` writes `config.responder.error_status = :unprocessable_content` (on Rack 3.1+) and `config.responder.redirect_status = :see_other`, but an old initializer lacks them, so a wrong password re-renders the form with 200 and Turbo does not show it. Add both lines (they need responders >= 3.1.0); the failure app's re-render of `sessions#new` uses the same `error_status`. Then fix what Devise 5 removed: `devise_error_messages!` becomes `render "devise/shared/error_messages", resource: resource`, `Devise::TestHelpers` becomes `Devise::Test::ControllerHelpers`, and `sign_in(user, bypass: true)` becomes `bypass_sign_in(user)`. Sign-out is still `DELETE` by default, so links using rails-ujs `method: :delete` must become `button_to` or `data: { turbo_method: :delete }`. Also check that `Devise.secret_key` now falls back only to `secret_key_base`.

code

ruby · 6 lines
ruby
# config/initializers/devise.rb (lines an older initializer lacks)
Devise.setup do |config|
  config.responder.error_status = :unprocessable_content
  config.responder.redirect_status = :see_other
  config.sign_out_via = :delete
end

go deeper

for a junior

Know that Devise errors must come back as 422 and redirects as 303 for Turbo, and that sign-out is a DELETE.

for a middle

Explain the library versus generated responder defaults, where the failure app takes its status from, and which Devise 5 helpers were removed.

for a senior

Run the upgrade end to end: initializer settings, responders version, sign-out markup, removed APIs, the secret-key change and a Turbo system test.

for a principal

Plan gem majors so behaviour-changing defaults are adopted deliberately, with a checklist and tests rather than discovered by users.

## The symptom An app started on Devise 4 with rails-ujs is upgraded to **Devise 5** on **Rails 8.1** with **Turbo**. A wrong password on the sign-in form now does nothing visible: the request returns, the page does not change, no "Invalid email or password" appears. Sign-up validation errors behave the same, and the old "Sign out" link stopped working too. ## Cause 1: the responder statuses Devise renders and redirects through `Devise::Controllers::Responder`, a subclass of the responders gem's `ActionController::Responder`. Its library defaults are: | Setting | Library default | Generated initializer | |---|---|---| | `error_status` | `:ok` (200) | `:unprocessable_content` (422) on Rack 3.1+, `:unprocessable_entity` on older Rack | | `redirect_status` | `:found` (302) | `:see_other` (303) | Only `config/initializers/devise.rb` files written by a recent `rails generate devise:install` contain the two `config.responder` lines. An initializer kept from an older Devise does not, and the upgrade does not change the defaults, so the app keeps answering 200 for a re-rendered form. Turbo Drive does not render a 200 response to a form submission (why is the Turbo form-response rule's subject), so the error page is silently discarded. The failed sign-in path makes this concrete: 1. `Devise::SessionsController#create` calls `warden.authenticate!(auth_options)`, where `auth_options` includes `recall: "#{controller_path}#new"`. 2. The strategy fails, Warden throws, and `Devise::FailureApp` takes its **recall** branch: it re-runs `sessions#new` with a flash alert. 3. It then sets the response status to `Devise.responder.error_status` (or `redirect_status` if the recalled action redirected). With the library default that is **200**. The fix is two lines in the initializer, `config.responder.error_status = :unprocessable_content` and `config.responder.redirect_status = :see_other`, and a `responders` gem at **3.1.0 or later**; older versions print a warning and ignore them. Use `:unprocessable_content`: on Rack 3.1 and later `:unprocessable_entity` is Rack's obsolete name. Rails and Devise's failure app translate it before it reaches Rack, but any code that hands it to Rack directly triggers Rack's deprecation warning. ## Cause 2: sign-out without rails-ujs `config.sign_out_via` is `:delete`, so `devise_for` routes `destroy_user_session` only for `DELETE`. Devise's shared views provide no sign-out link; apps wrote `link_to "Sign out", destroy_user_session_path, method: :delete`, which needed rails-ujs. With Turbo: - `button_to "Sign out", destroy_user_session_path, method: :delete` works because it renders a real form; - a link needs `data: { turbo_method: :delete }`. `Devise::SessionsController#destroy` then redirects with `Devise.responder.redirect_status`, which is 303 once configured. ## Cause 3: APIs Devise 5 removed Devise 5.0 removed several deprecated APIs that old apps often still call: - `devise_error_messages!` in copied views: replace with `render "devise/shared/error_messages", resource: resource`. The partial now marks its container `data-turbo-temporary` so Turbo does not cache it. - `Devise::TestHelpers`: use `Devise::Test::ControllerHelpers` (controller and view tests) or `Devise::Test::IntegrationHelpers` (integration and request tests). - `sign_in(user, bypass: true)`: use `bypass_sign_in(user)`, typically after a password change. - The positional scope `sign_in(user, :admin)` in tests: use `sign_in(user, scope: :admin)`. ## What already works with Turbo Not everything needs changing. `Devise.navigational_formats` defaults to `["*/*", :html, :turbo_stream]`, so a Turbo request that hits `authenticate_user!` is redirected to the sign-in page like any browser request instead of receiving a 401. The shared views Devise 5 ships already use Turbo-era markup, such as `data: { turbo: false }` on OmniAuth buttons. Apps that copied Devise's views years ago keep the old markup until they re-copy or edit them, which is why upgrades often fix the initializer and still show stale views. Elsewhere in the copied views, replace `data-confirm` with `data-turbo-confirm`. ## Cause 4: the secret key Devise 5 removed its secret-key finder. `Devise.secret_key` now defaults to the app's `secret_key_base` only. If the old app's lookup found a different value, outstanding reset, unlock and confirmation tokens stop working after the deploy; set `config.secret_key` explicitly if you need continuity. ## Upgrade checklist 1. Add the two `config.responder` lines and bump `responders`. 2. Replace `method: :delete` sign-out links and `data-confirm` with their Turbo forms. 3. Grep views and tests for `devise_error_messages!`, `Devise::TestHelpers`, `bypass: true`. 4. Confirm which secret signed existing tokens. 5. Run the sign-in, sign-up, password-reset and sign-out flows in a system test with Turbo enabled.

  • Why does Devise not simply make 422 and 303 the library defaults in 5.0?
    Changing the default would alter the status every existing app returns without any code change, which can break clients or tests that expect 200 and 302. Devise keeps the old defaults and writes the new values into freshly generated initializers; its comment says they may become the default later. Upgraders opt in by adding the two lines.
  • When would you call bypass_sign_in instead of sign_in?
    After the signed-in user changes their password in your own controller. Devise's session entry holds a salt cut from `encrypted_password`, so a new password makes the stored session stop matching and the user would be signed out. `bypass_sign_in(user)` re-stores them through the session serializer without Warden callbacks such as trackable, unlike a normal `sign_in`; Devise's own registrations controller does the same.

  • Upgrading the gem to Devise 5 changes the responder defaults to 422 and 303
  • status :unprocessable_entity is the current name to set on Rack 3.2
  • link_to with method: :delete works under Turbo without rails-ujs
  • devise_error_messages! still works in Devise 5 with a deprecation warning
  • Devise::TestHelpers is still the module to include in controller tests

Assumes Devise 5.0 on Rails 8.1 with Rack 3.1 or later; Devise 5 removed devise_error_messages!, Devise::TestHelpers and sign_in bypass:, and its library responder defaults are still 200 and 302.