From 491c6cea4a0d326e363f805afb7254d6ef0270c9 Mon Sep 17 00:00:00 2001 From: "Ben A. Morgan" <145508870+BenMorganMY@users.noreply.github.com> Date: Fri, 25 Oct 2024 14:44:26 -0600 Subject: [PATCH 1/2] call to_hash on inertia_errors --- lib/inertia_rails/controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/inertia_rails/controller.rb b/lib/inertia_rails/controller.rb index 34bf9d0..c050ac5 100644 --- a/lib/inertia_rails/controller.rb +++ b/lib/inertia_rails/controller.rb @@ -109,7 +109,7 @@ def inertia_location(url) def capture_inertia_errors(options) if (inertia_errors = options.dig(:inertia, :errors)) - session[:inertia_errors] = inertia_errors + session[:inertia_errors] = inertia_errors.to_hash end end end From 3aeeadd68a92118281864e94c1c541462cc9ca80 Mon Sep 17 00:00:00 2001 From: "Ben A. Morgan" <145508870+BenMorganMY@users.noreply.github.com> Date: Fri, 25 Oct 2024 15:06:58 -0600 Subject: [PATCH 2/2] add spec for serializing error object --- spec/dummy/app/controllers/inertia_test_controller.rb | 8 ++++++++ spec/dummy/config/routes.rb | 1 + spec/inertia/response_spec.rb | 9 +++++++++ 3 files changed, 18 insertions(+) diff --git a/spec/dummy/app/controllers/inertia_test_controller.rb b/spec/dummy/app/controllers/inertia_test_controller.rb index c4e9b0c..fb3f390 100644 --- a/spec/dummy/app/controllers/inertia_test_controller.rb +++ b/spec/dummy/app/controllers/inertia_test_controller.rb @@ -1,3 +1,7 @@ +class MyError + def to_hash() { uh: 'oh' } end +end + class InertiaTestController < ApplicationController layout 'conditional', only: [:with_different_layout] @@ -43,6 +47,10 @@ def redirect_with_inertia_errors redirect_to empty_test_path, inertia: { errors: { uh: 'oh' } } end + def redirect_with_inertia_error_object + redirect_to empty_test_path, inertia: { errors: MyError.new } + end + def redirect_back_with_inertia_errors redirect_back( fallback_location: empty_test_path, diff --git a/spec/dummy/config/routes.rb b/spec/dummy/config/routes.rb index 8faf2f9..5863d48 100644 --- a/spec/dummy/config/routes.rb +++ b/spec/dummy/config/routes.rb @@ -22,6 +22,7 @@ get 'share_multithreaded_error' => 'inertia_multithreaded_share#share_multithreaded_error' get 'redirect_with_inertia_errors' => 'inertia_test#redirect_with_inertia_errors' post 'redirect_with_inertia_errors' => 'inertia_test#redirect_with_inertia_errors' + post 'redirect_with_inertia_error_object' => 'inertia_test#redirect_with_inertia_error_object' post 'redirect_back_with_inertia_errors' => 'inertia_test#redirect_back_with_inertia_errors' get 'error_404' => 'inertia_test#error_404' get 'error_500' => 'inertia_test#error_500' diff --git a/spec/inertia/response_spec.rb b/spec/inertia/response_spec.rb index 0f0b74e..ea00b1a 100644 --- a/spec/inertia/response_spec.rb +++ b/spec/inertia/response_spec.rb @@ -27,6 +27,15 @@ expect(response.headers['Location']).to eq(empty_test_url) expect(session[:inertia_errors]).to include({ uh: 'oh' }) end + + it 'serializes :inertia_errors to the session' do + post redirect_with_inertia_error_object_path, + headers: { 'X-Inertia' => true } + + expect(response.status).to eq 302 + expect(response.headers['Location']).to eq(empty_test_url) + expect(session[:inertia_errors]).to include({ uh: 'oh' }) + end end end end