-
Notifications
You must be signed in to change notification settings - Fork 75
Open
Description
Problem
I'm encountering an issue with Inertia's forceFormData behavior when submitting file arrays in a Rails app:
@inertiajs/react: 2.0.17
inertia-rails: 3.10
When submitting a payload like:
{
product: {
name: "Test",
images: [File1, File2, File3]
}
}
with:
router.post('/products', data, { forceFormData: true })
Inertia serializes it into indexed keys:
product[images][0]: <File1>
product[images][1]: <File2>
product[images][2]: <File3>
Rails parses this into:
params[:product][:images] # => { "0" => ..., "1" => ..., ... }
…which fails the strong parameters declaration:
params.require(:product).permit(:name, images: [])
The images field gets filtered out and validation errors follow.
Expected Behavior
FormData keys should be serialized as:
product[images][]: <File1>
product[images][]: <File2>
This results in a valid array in Rails:
params[:product][:images] # => [<UploadedFile>, <UploadedFile>, ...]
Temporary Workaround
We’re currently patching the input server-side:
def normalize_images_param
images = params.dig(:product, :images)
return unless images.is_a?(ActionController::Parameters)
params[:product][:images] = images.values
end
This converts the hash back into an array before permitting the field.
Metadata
Metadata
Assignees
Labels
No labels