Skip to content

form_for generates duplicate ids for form fields when used in loop #2971

@asanghi

Description

@asanghi

It is arguably a common practice to loop over a collection of models and create a form for each object within that loop.

If form_for is used to create the form, unique ids are generated for the form tag itself. The individual form fields are however not that lucky. Basically #object_name_method_name is repeated for each field within the form. This leads to all sorts of javascripty malfunctions (datepicker on the last form will populate the first form's date field) besides making the generated HTML invalid (which in itself is not a problem for some pragmatic folks who disregard its importance). Click on the label of a field in the the 5th form and focus goes to the corresponding field in the 1st form.

I propose that it be possible to namespace each form using a :namespace option in the form.

If there is a simpler solution that i'm not privy to, please do let me know. If the idea is good and acceptable to the powers that be, then patches can be offered too.

FWIW, formtastic gem was extended to include a namespace option for precisely this use case. I'm not currently using formtastic for this project, but in need of an elegant solution that does not require me to pollute each form field (and the label) with an id.

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