Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 590
digitalWestie Avatar asked Feb 07 '11 13:02

digitalWestie


People also ask

What is a form helper in Rails?

Rails provides a series of helpers for generating form elements such as checkboxes, text fields, and radio buttons. These basic helpers, with names ending in _tag (such as text_field_tag and check_box_tag ), generate just a single <input> element. The first parameter to these is always the name of the input.

What is form in Rails?

Forms in web applications are an essential interface for user input. However, form markup can quickly become tedious to write and maintain because of the need to handle form control naming and its numerous attributes. Rails does away with this complexity by providing view helpers for generating form markup.


1 Answers

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]

like image 199
CoderDave Avatar answered Sep 25 '22 22:09

CoderDave