Skip to content

Commit d6715c7

Browse files
committed
Scaffold destroy action returns status code 303
The default controller scaffolding currently sends back a 302 for its destroy action. Since Hotwire is the default for Rails going forward, this can be problimatic. Turbo uses the fetch API internally which is particular on how it handles redirects. As outlined in this table in this Turbo issue, hotwired/turbo#84 (comment), Turbo making a DELETE request (not POST + hidden _method field) and recieving a 302 back will result in another DELETE request instead of a GET request for the redirect. This updates the controller template used for the scaffold generator to send back the 303 see other status code for the destroy action. For consistancy-sake, the Action Controller Overview guide examples were also updated. The main Getting Started guide page already has see other used in its example destroy action. For non-Hotwire users, the browser will still properly redirect on a 303 as it does with a 302.
1 parent f2cafdd commit d6715c7

File tree

4 files changed

+9
-3
lines changed

4 files changed

+9
-3
lines changed

guides/source/action_controller_overview.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,7 @@ class LoginsController < ApplicationController
486486
session.delete(:current_user_id)
487487
# Clear the memoized current user
488488
@_current_user = nil
489-
redirect_to root_url
489+
redirect_to root_url, status: :see_other
490490
end
491491
end
492492
```
@@ -508,7 +508,7 @@ class LoginsController < ApplicationController
508508
def destroy
509509
session.delete(:current_user_id)
510510
flash[:notice] = "You have successfully logged out."
511-
redirect_to root_url
511+
redirect_to root_url, status: :see_other
512512
end
513513
end
514514
```

railties/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
* Send 303 See Other status code back for the destroy action on newly generated
2+
scaffold controllers.
3+
4+
*Tony Drake*
5+
16
* Add `Rails.application.deprecators` as a central point to manage deprecators
27
for an application.
38

railties/lib/rails/generators/rails/scaffold_controller/templates/controller.rb.tt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class <%= controller_class_name %>Controller < ApplicationController
4343
# DELETE <%= route_url %>/1
4444
def destroy
4545
@<%= orm_instance.destroy %>
46-
redirect_to <%= index_helper %>_url, notice: <%= %("#{human_name} was successfully destroyed.") %>
46+
redirect_to <%= index_helper %>_url, notice: <%= %("#{human_name} was successfully destroyed.") %>, status: :see_other
4747
end
4848

4949
private

railties/test/generators/scaffold_controller_generator_test.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ def test_controller_skeleton_is_created
4444
assert_instance_method :destroy, content do |m|
4545
assert_match(/@user\.destroy/, m)
4646
assert_match(/User was successfully destroyed/, m)
47+
assert_match(/status: :see_other/, m)
4748
end
4849

4950
assert_instance_method :set_user, content do |m|

0 commit comments

Comments
 (0)