massaskillz
massaskillz

Reputation: 283

Is there a Rails way to automatically generate sequential IDs for fields of the same type?

I have a form that will generate an XML file. There are multiple fields for "items" that I would like to manipulate individually with JQuery using the ID as the selector. I have the following HAML code to generate the fields:

- 5.times do
  = f.text_field :item, multiple: true, class: 'form-control'

Is there a Rails way to automatically set the IDs to item_1, item_2, item_3, etc.?

Upvotes: 0

Views: 51

Answers (1)

memlucky
memlucky

Reputation: 105

You can do something like this

- 5.times do |x|
    = f.text_field "item_#{x+1}", multiple: true, class: 'form-control'

Upvotes: 1

Related Questions