How do I get the HTML 'name' attribute a rails form builder will generate for a certain field?

digitalWestie picture digitalWestie · Feb 7, 2011 · Viewed 10.7k times · Source

When you've got a form field such as this:

<%= f.text_field :last_name %>

it will generate this in HTML:

<input id="person_last_name" name="person[last_name]" size="30" type="text" />

I'd like to know if there's any way to get the name attribute (in this case "person[last_name]") that will be generated.

It seems a bit of an odd thing to want to get but I've got my reasons! I also can't be bothered launching into a lengthy explanation too.

Answer

CoderDave picture CoderDave · Oct 2, 2013

After inspecting the form object, I found that you can get the object_name from it.

So this worked well for me: "#{f.object_name}[field_name]"

Which will generate: object[object_attributes][0][field_name]