Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 997
Arnold Roa Avatar asked May 10 '26 15:05

Arnold Roa


1 Answers

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.

like image 174
MrShemek Avatar answered May 12 '26 04:05

MrShemek