Arnold Roa
Arnold Roa

Reputation: 7748

duplicated id for fields using simple_form

I've a form in my page for each record, something like this:

- records.each do |r|
  ...
  = simple_form_for record do |f|
    = f.input :field

The problem is that each input has the same id. How can I add a prefix to generate ids like record_12_field? I've tried input_html and html options on the simple_form_for attributes but doesn't work.

The only alternative I can think of is manually setting the id for each input.

This is different to the duplication suggested, because i'm not asking how to set an id for a single field, but how to add a prefix to all of them.

The answer is using the namespace option, this answer does not appear on the duplicate. Please allow me to answer this.

Upvotes: 3

Views: 875

Answers (1)

MrShemek
MrShemek

Reputation: 2483

Update
As Arnold mentioned, it is possible with the namespace option.

Original answer
I think it is not possible to add the prefix automatically. As a workaround you can try with:

- records.each do |record|
  ...
  = simple_form_for record do |f|
    = f.input :field, input_html: { id: "record_#{record.id}_field" }

Alternatively, you can switch to form_for and use this solution.

Upvotes: 3

Related Questions