Skip to content

Commit bdf9a2d

Browse files
authored
Merge pull request #143 from BenMorganMY/fix-inertia-errors
Call to_hash On inertia_errors for Session Serialization
2 parents b2701ec + 3aeeadd commit bdf9a2d

File tree

4 files changed

+19
-1
lines changed

4 files changed

+19
-1
lines changed

lib/inertia_rails/controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ def inertia_location(url)
109109

110110
def capture_inertia_errors(options)
111111
if (inertia_errors = options.dig(:inertia, :errors))
112-
session[:inertia_errors] = inertia_errors
112+
session[:inertia_errors] = inertia_errors.to_hash
113113
end
114114
end
115115
end

spec/dummy/app/controllers/inertia_test_controller.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
class MyError
2+
def to_hash() { uh: 'oh' } end
3+
end
4+
15
class InertiaTestController < ApplicationController
26
layout 'conditional', only: [:with_different_layout]
37

@@ -43,6 +47,10 @@ def redirect_with_inertia_errors
4347
redirect_to empty_test_path, inertia: { errors: { uh: 'oh' } }
4448
end
4549

50+
def redirect_with_inertia_error_object
51+
redirect_to empty_test_path, inertia: { errors: MyError.new }
52+
end
53+
4654
def redirect_back_with_inertia_errors
4755
redirect_back(
4856
fallback_location: empty_test_path,

spec/dummy/config/routes.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
get 'share_multithreaded_error' => 'inertia_multithreaded_share#share_multithreaded_error'
2424
get 'redirect_with_inertia_errors' => 'inertia_test#redirect_with_inertia_errors'
2525
post 'redirect_with_inertia_errors' => 'inertia_test#redirect_with_inertia_errors'
26+
post 'redirect_with_inertia_error_object' => 'inertia_test#redirect_with_inertia_error_object'
2627
post 'redirect_back_with_inertia_errors' => 'inertia_test#redirect_back_with_inertia_errors'
2728
get 'error_404' => 'inertia_test#error_404'
2829
get 'error_500' => 'inertia_test#error_500'

spec/inertia/response_spec.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,15 @@
2727
expect(response.headers['Location']).to eq(empty_test_url)
2828
expect(session[:inertia_errors]).to include({ uh: 'oh' })
2929
end
30+
31+
it 'serializes :inertia_errors to the session' do
32+
post redirect_with_inertia_error_object_path,
33+
headers: { 'X-Inertia' => true }
34+
35+
expect(response.status).to eq 302
36+
expect(response.headers['Location']).to eq(empty_test_url)
37+
expect(session[:inertia_errors]).to include({ uh: 'oh' })
38+
end
3039
end
3140
end
3241
end

0 commit comments

Comments
 (0)