Skip to content

Commit f576854

Browse files
authored
Add redirect_back w/ inertia errors support (#56)
This builds on the work in 40f36a5 which added an inertia-aware wrapper around `redirect_to`. This change implements the same inertia errors handling for `redirect_back`, which I've found to be particularly useful when working with Inertia, especially with error handling.
1 parent 1e51f62 commit f576854

File tree

4 files changed

+44
-3
lines changed

4 files changed

+44
-3
lines changed

lib/inertia_rails/controller.rb

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,31 @@ def inertia_share(**args, &block)
2020
end
2121
end
2222

23+
private
24+
2325
def inertia_location(url)
2426
headers['X-Inertia-Location'] = url
2527
head :conflict
2628
end
2729

2830
def redirect_to(options = {}, response_options = {})
29-
if (inertia_errors = response_options.fetch(:inertia, {}).fetch(:errors, nil))
31+
capture_inertia_errors(response_options)
32+
super(options, response_options)
33+
end
34+
35+
def redirect_back(fallback_location:, allow_other_host: true, **options)
36+
capture_inertia_errors(options)
37+
super(
38+
fallback_location: fallback_location,
39+
allow_other_host: allow_other_host,
40+
**options,
41+
)
42+
end
43+
44+
def capture_inertia_errors(options)
45+
if (inertia_errors = options.dig(:inertia, :errors))
3046
session[:inertia_errors] = inertia_errors
3147
end
32-
33-
super(options, response_options)
3448
end
3549
end
3650
end

spec/dummy/app/controllers/inertia_test_controller.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@ def redirect_with_inertia_errors
3333
redirect_to empty_test_path, inertia: { errors: { uh: 'oh' } }
3434
end
3535

36+
def redirect_back_with_inertia_errors
37+
redirect_back(
38+
fallback_location: empty_test_path,
39+
inertia: { errors: { go: 'back!' } }
40+
)
41+
end
42+
3643
def error_404
3744
render inertia: 'ErrorComponent', status: 404
3845
end

spec/dummy/config/routes.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
get 'share_multithreaded' => 'inertia_multithreaded_share#share_multithreaded'
1919
get 'redirect_with_inertia_errors' => 'inertia_test#redirect_with_inertia_errors'
2020
post 'redirect_with_inertia_errors' => 'inertia_test#redirect_with_inertia_errors'
21+
post 'redirect_back_with_inertia_errors' => 'inertia_test#redirect_back_with_inertia_errors'
2122
get 'error_404' => 'inertia_test#error_404'
2223
get 'error_500' => 'inertia_test#error_500'
2324
get 'content_type_test' => 'inertia_test#content_type_test'

spec/inertia/response_spec.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,23 @@
3030
end
3131
end
3232
end
33+
34+
describe 'redirect_back' do
35+
context 'with an [:inertia][:errors] option' do
36+
context 'with a post request' do
37+
it 'adds :inertia_errors to the session' do
38+
post(
39+
redirect_back_with_inertia_errors_path,
40+
headers: {
41+
'X-Inertia' => true,
42+
'HTTP_REFERER' => "http://example.com/current-path"
43+
}
44+
)
45+
expect(response.status).to eq 302
46+
expect(response.headers['Location']).to eq('http://example.com/current-path')
47+
expect(session[:inertia_errors]).to include({ go: 'back!' })
48+
end
49+
end
50+
end
51+
end
3352
end

0 commit comments

Comments
 (0)