Skip to content

Inertia forceFormData sends indexed keys ([0], [1]) instead of [], causing issues with Rails strong params #252

@florianfelsing

Description

@florianfelsing

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

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions