Skip to content

Commit

Permalink
Merge pull request #143 from BenMorganMY/fix-inertia-errors
Browse files Browse the repository at this point in the history
Call to_hash On inertia_errors for Session Serialization
  • Loading branch information
bknoles authored Oct 30, 2024
2 parents b2701ec + 3aeeadd commit bdf9a2d
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/inertia_rails/controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions spec/dummy/app/controllers/inertia_test_controller.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
class MyError
def to_hash() { uh: 'oh' } end
end

class InertiaTestController < ApplicationController
layout 'conditional', only: [:with_different_layout]

Expand Down Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions spec/dummy/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,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'
Expand Down
9 changes: 9 additions & 0 deletions spec/inertia/response_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit bdf9a2d

Please sign in to comment.