What's best practice for assigning unique ids to Rails form inputs?

Viewed 60

I have a Rails form for multi-editing. The purpose of this form is to be able to edit attributes for multiple invite objects.

This form looks like:

= form_with url: some_path
  - invites.each do |invite|
    = fields_for 'invites[]', invite do |invite_form|
      = invite_form.email :email
      = invite_form.select :role

Due to the fact that all of these inputs are nested under a single form, the ids generated for these inputs are not unique which is problematic for accessibility.

It seems like if I had multiple forms, I could make use of setting namespace, but what's standard for setting the id for something like this where there are duplicate input types under one form (but for different objects)? Is it bad practice to generate some random ids for setting the id? Or just using each_with_index and appending an index? To note, invites is not an active record model so there's no id I can make use of there.

0 Answers
Related