Skip to content

Commit f3bcd99

Browse files
authored
Merge pull request #146 from skryukov/support-redirect-back-or-to
Respect `raise_on_open_redirects` Rails 7.0+ config
2 parents bdf9a2d + 0da7359 commit f3bcd99

File tree

4 files changed

+32
-12
lines changed

4 files changed

+32
-12
lines changed

lib/inertia_rails/controller.rb

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,7 @@ def default_render
6767

6868
def redirect_to(options = {}, response_options = {})
6969
capture_inertia_errors(response_options)
70-
super(options, response_options)
71-
end
72-
73-
def redirect_back(fallback_location:, allow_other_host: true, **options)
74-
capture_inertia_errors(options)
75-
super(
76-
fallback_location: fallback_location,
77-
allow_other_host: allow_other_host,
78-
**options,
79-
)
70+
super
8071
end
8172

8273
private

spec/dummy/app/controllers/inertia_test_controller.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,13 @@ def redirect_back_with_inertia_errors
5858
)
5959
end
6060

61+
def redirect_back_or_to_with_inertia_errors
62+
redirect_back_or_to(
63+
empty_test_path,
64+
inertia: { errors: { go: 'back!' } }
65+
)
66+
end
67+
6168
def error_404
6269
render inertia: 'ErrorComponent', status: 404
6370
end

spec/dummy/config/routes.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
post 'redirect_with_inertia_errors' => 'inertia_test#redirect_with_inertia_errors'
2626
post 'redirect_with_inertia_error_object' => 'inertia_test#redirect_with_inertia_error_object'
2727
post 'redirect_back_with_inertia_errors' => 'inertia_test#redirect_back_with_inertia_errors'
28+
post 'redirect_back_or_to_with_inertia_errors' => 'inertia_test#redirect_back_or_to_with_inertia_errors'
2829
get 'error_404' => 'inertia_test#error_404'
2930
get 'error_500' => 'inertia_test#error_500'
3031
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
@@ -48,11 +48,32 @@
4848
redirect_back_with_inertia_errors_path,
4949
headers: {
5050
'X-Inertia' => true,
51-
'HTTP_REFERER' => "http://example.com/current-path"
51+
'HTTP_REFERER' => "http://www.example.com/current-path"
5252
}
5353
)
5454
expect(response.status).to eq 302
55-
expect(response.headers['Location']).to eq('http://example.com/current-path')
55+
expect(response.headers['Location']).to eq('http://www.example.com/current-path')
56+
expect(session[:inertia_errors]).to include({ go: 'back!' })
57+
end
58+
end
59+
end
60+
end
61+
62+
describe 'redirect_back_or_to' do
63+
context 'with an [:inertia][:errors] option' do
64+
context 'with a post request' do
65+
it 'adds :inertia_errors to the session' do
66+
skip("Requires Rails 7.0 or higher") if Rails.version < '7'
67+
68+
post(
69+
redirect_back_or_to_with_inertia_errors_path,
70+
headers: {
71+
'X-Inertia' => true,
72+
'HTTP_REFERER' => "http://www.example.com/current-path"
73+
}
74+
)
75+
expect(response.status).to eq 302
76+
expect(response.headers['Location']).to eq('http://www.example.com/current-path')
5677
expect(session[:inertia_errors]).to include({ go: 'back!' })
5778
end
5879
end

0 commit comments

Comments
 (0)