Skip to content

Commit 3f4e940

Browse files
committed
Add support for redirect_back_or_to and respect raise_on_open_redirects config
1 parent 56dfc76 commit 3f4e940

File tree

4 files changed

+39
-9
lines changed

4 files changed

+39
-9
lines changed

lib/inertia_rails/controller.rb

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,17 @@ def default_render
6767

6868
def redirect_to(options = {}, response_options = {})
6969
capture_inertia_errors(response_options)
70-
super(options, response_options)
70+
super
7171
end
7272

73-
def redirect_back(fallback_location:, allow_other_host: true, **options)
73+
def redirect_back(**options)
7474
capture_inertia_errors(options)
75-
super(
76-
fallback_location: fallback_location,
77-
allow_other_host: allow_other_host,
78-
**options,
79-
)
75+
super
76+
end
77+
78+
def redirect_back_or_to(_fallback_location, **options)
79+
capture_inertia_errors(options)
80+
super
8081
end
8182

8283
private

spec/dummy/app/controllers/inertia_test_controller.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ def redirect_back_with_inertia_errors
5050
)
5151
end
5252

53+
def redirect_back_or_to_with_inertia_errors
54+
redirect_back_or_to(
55+
empty_test_path,
56+
inertia: { errors: { go: 'back!' } }
57+
)
58+
end
59+
5360
def error_404
5461
render inertia: 'ErrorComponent', status: 404
5562
end

spec/dummy/config/routes.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
get 'redirect_with_inertia_errors' => 'inertia_test#redirect_with_inertia_errors'
2525
post 'redirect_with_inertia_errors' => 'inertia_test#redirect_with_inertia_errors'
2626
post 'redirect_back_with_inertia_errors' => 'inertia_test#redirect_back_with_inertia_errors'
27+
post 'redirect_back_or_to_with_inertia_errors' => 'inertia_test#redirect_back_or_to_with_inertia_errors'
2728
get 'error_404' => 'inertia_test#error_404'
2829
get 'error_500' => 'inertia_test#error_500'
2930
get 'content_type_test' => 'inertia_test#content_type_test'

spec/inertia/response_spec.rb

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,32 @@
3939
redirect_back_with_inertia_errors_path,
4040
headers: {
4141
'X-Inertia' => true,
42-
'HTTP_REFERER' => "http://example.com/current-path"
42+
'HTTP_REFERER' => "http://www.example.com/current-path"
4343
}
4444
)
4545
expect(response.status).to eq 302
46-
expect(response.headers['Location']).to eq('http://example.com/current-path')
46+
expect(response.headers['Location']).to eq('http://www.example.com/current-path')
47+
expect(session[:inertia_errors]).to include({ go: 'back!' })
48+
end
49+
end
50+
end
51+
end
52+
53+
describe 'redirect_back_or_to' do
54+
context 'with an [:inertia][:errors] option' do
55+
context 'with a post request' do
56+
it 'adds :inertia_errors to the session' do
57+
skip("Requires Rails 7.0 or higher") if Rails.version < '7'
58+
59+
post(
60+
redirect_back_or_to_with_inertia_errors_path,
61+
headers: {
62+
'X-Inertia' => true,
63+
'HTTP_REFERER' => "http://www.example.com/current-path"
64+
}
65+
)
66+
expect(response.status).to eq 302
67+
expect(response.headers['Location']).to eq('http://www.example.com/current-path')
4768
expect(session[:inertia_errors]).to include({ go: 'back!' })
4869
end
4970
end

0 commit comments

Comments
 (0)