Rails: Using form_for multiple times (DOM ids)

iGEL picture iGEL · Jun 21, 2011 · Viewed 8.4k times · Source

I would like to use the form_for helper multiple times for the same model in the same page. But the input fields use the same ID attribute (in the HTML), so clicking on the label of a field in another form will select the same input in the first form.

Is there a solution besides settings all attributes manually via :for => "title_#{item.id}" and :id => "title_#{item.id}"?

Using Rails 3.0.9

Answer

Marc Schütz picture Marc Schütz · Mar 30, 2012

You can use :namespace => 'some_unique_prefix' option. In contrast to :index, this will not change the value used in the name attribute.

It's also possible to use an array, e.g. when you have nested forms or different forms that happen to have some fields in common: :namespace => [@product.id, tag.id] or :namespace => [:product, @product.id]