ruby-on-railsrubyformsactionview

What is the reasoning for having form_with not generate any ids?


The form_with helper doesn't generate ids for form elements, and therefore doesn't generate for attributes either. This differs from the old form_tag and form_for helpers.

If you want to use form_with instead of the deprecated form_tag and form_for helpers, but you want ids to generate, you need to add this to your config:

config.action_view.form_with_generates_ids = true

id generation is useful in some cases because certain front-end things may require it. On top of that, it seems to me that not generating for attributes means that forms generated with form_with have less a11y.

I'm currently working in an older codebase where form element ids are required, and my knee-jerk reaction is to enable the above config setting so I can use form_with without having to manually set IDs for every element.

What is the reasoning for making form_with not generate ids by default? I'm concerned that I'm missing something here, since I assume there's a good reason for the decision.


Solution

  • From Rails 5.2 onwards, that actually is the default:

    config.action_view.form_with_generates_ids = true
    

    You can see it in the release notes, along with the commit that changed it. From the description of that commit:

    When form_with was introduced we disabled the automatic generation of ids that was enabled in form_for. This usually is not an good idea since labels don't work when the input doesn't have an id and it made harder to test with Capybara. You can still disable the automatic generation of ids setting config.action_view.form_with_generates_ids to false.

    Doesn't seem like you're missing anything :D