File tree Expand file tree Collapse file tree 4 files changed +44
-3
lines changed Expand file tree Collapse file tree 4 files changed +44
-3
lines changed Original file line number Diff line number Diff line change @@ -20,17 +20,31 @@ def inertia_share(**args, &block)
20
20
end
21
21
end
22
22
23
+ private
24
+
23
25
def inertia_location ( url )
24
26
headers [ 'X-Inertia-Location' ] = url
25
27
head :conflict
26
28
end
27
29
28
30
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 ) )
30
46
session [ :inertia_errors ] = inertia_errors
31
47
end
32
-
33
- super ( options , response_options )
34
48
end
35
49
end
36
50
end
Original file line number Diff line number Diff line change @@ -33,6 +33,13 @@ def redirect_with_inertia_errors
33
33
redirect_to empty_test_path , inertia : { errors : { uh : 'oh' } }
34
34
end
35
35
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
+
36
43
def error_404
37
44
render inertia : 'ErrorComponent' , status : 404
38
45
end
Original file line number Diff line number Diff line change 18
18
get 'share_multithreaded' => 'inertia_multithreaded_share#share_multithreaded'
19
19
get 'redirect_with_inertia_errors' => 'inertia_test#redirect_with_inertia_errors'
20
20
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'
21
22
get 'error_404' => 'inertia_test#error_404'
22
23
get 'error_500' => 'inertia_test#error_500'
23
24
get 'content_type_test' => 'inertia_test#content_type_test'
Original file line number Diff line number Diff line change 30
30
end
31
31
end
32
32
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
33
52
end
You can’t perform that action at this time.
0 commit comments