When using the form_for
helper and a text_field
call, Ruby on Rails will generate a unique id for the element that it outp
In case someone has a FormBuilder object from a fields_for
block, it is possible to get its id
using this snippet:
<%= form.fields_for :something do |fields_form| %>
<%= fields_form.object_name.gsub(/[\[\]]+/, '_').chop %>id
<% end %>
FieldsForm#object_name
returns the field's ID as something like this: user[address][0]
. Next, the regex substitution changes groups of one or more brackets to underscores. This substitution leaves a trailing underscore, to which it appends the letters id
. For the example provided before, this results in user_address_0_id
.