Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Call to_hash On inertia_errors for Session Serialization #143

Merged
merged 2 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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'
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