Skip to content

Commit 59abee8

Browse files
maebealeclaudejmilljr24
authored
Configure Devise responder for Turbo compatibility (#1444)
* Configure Devise responder for Turbo compatibility Devise 5 supports Turbo-aware status codes natively via responder config. Set error_status to :unprocessable_entity and redirect_status to :see_other so Turbo can handle Devise form responses correctly, and remove the data-turbo=false workarounds from login, confirmation, and welcome forms. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * flash error message * fix flash messages --------- Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com> Co-authored-by: Justin Miller <16829344+jmilljr24@users.noreply.github.com>
1 parent 7c1104b commit 59abee8

5 files changed

Lines changed: 20 additions & 9 deletions

File tree

app/controllers/confirmations_controller.rb

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,19 @@ def show
2121
end
2222
end
2323

24+
2425
# POST /users/confirmation (resend)
2526
def create
2627
self.resource = resource_class.send_confirmation_instructions(resource_params)
2728

28-
if resource.errors.empty?
29-
redirect_to new_user_session_path,
30-
notice: "If your email exists in our system, you will receive confirmation instructions shortly."
29+
if resource.errors.empty? || email_not_found?
30+
flash[:notice] = "If your email exists in our system, you will receive confirmation instructions shortly."
31+
redirect_to new_user_session_path
3132
elsif resource.confirmed_at.present?
32-
redirect_to new_user_session_path,
33-
notice: "Your email is already confirmed. Please sign in."
33+
flash[:notice] = "Your email is already confirmed. Please sign in."
34+
redirect_to new_user_session_path
3435
else
36+
flash.now[:alert] = resource.errors.full_messages.join(", ")
3537
respond_with(resource)
3638
end
3739
end
@@ -47,4 +49,10 @@ def after_confirmation_success(resource)
4749
redirect_to new_user_session_path, notice: "Your email has been confirmed. Please sign in."
4850
end
4951
end
52+
53+
private
54+
55+
def email_not_found?
56+
resource.errors.any? { |e| e.attribute == :email && e.type == :not_found }
57+
end
5058
end

app/views/devise/confirmations/new.html.erb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@
2424

2525
<div class="action-buttons mt-8 flex justify-center gap-3">
2626
<%= f.submit "Resend confirmation instructions",
27-
class: "btn btn-primary",
28-
data: { turbo: false} %>
27+
class: "btn btn-primary" %>
2928
</div>
3029
<% end %>
3130

app/views/devise/sessions/new.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
Log in to our Portal
88
</h2>
99

10-
<%= simple_form_for(resource, url: session_path(resource_name), data: { turbo: false }) do |f| %>
10+
<%= simple_form_for(resource, url: session_path(resource_name)) do |f| %>
1111
<% if resource.errors.any? %>
1212
<%= render 'shared/errors', resource: resource %>
1313
<% end %>

app/views/welcome/show.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
</div>
1414

1515
<%= simple_form_for(@user, url: user_welcome_update_path(@user.welcome_instructions_token),
16-
scope: :user, html: { class: "space-y-4" }, data: { turbo_method: :patch, turbo: false }) do |f| %>
16+
scope: :user, html: { class: "space-y-4" }) do |f| %>
1717
<% if @user.errors.any? %>
1818
<%= render 'shared/errors', resource: @user %>
1919
<% end %>

config/initializers/devise.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
config.mailer_sender = ENV.fetch("REPLY_TO_EMAIL", "programs@awbw.org")
1414
config.sign_in_after_reset_password = true
1515

16+
# Use Turbo-compatible HTTP status codes
17+
config.responder.error_status = :unprocessable_entity
18+
config.responder.redirect_status = :see_other
19+
1620
# Configure the class responsible to send e-mails.
1721
# config.mailer = 'Devise::Mailer'
1822
config.mailer = "DeviseMailer"

0 commit comments

Comments
 (0)