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

4

37

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.

Divvy answered 7/2, 2011 at 13:42 Comment(1)
It seems to be something in the line of "object[attribute]", no ?Dillingham
E
11

Well, as expected, the comment you have is quite true :)

The place in the source where this happens is the InstanceTag class to which all the tag generation drills down. The method is called tag_name.

Eiten answered 7/2, 2011 at 14:17 Comment(1)
Wow thanks! I was just about to dive into the source as well. I know that it's pretty much "object[attribute]" but things are a lot more complicated when you have nested model forms - one of the reasons I was looking for such a method.Divvy
H
38

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]

Harlen answered 2/10, 2013 at 16:54 Comment(1)
Even better, with Rails 4.0+, by using the :as option, you can specify object_name when you create the form (as long as you don't initialize with a string or symbol). For instance, <%= form_for(@object, as:'my_obj_name',...) will generate all fields as my_obj_name[field_name].Luane
E
11

Well, as expected, the comment you have is quite true :)

The place in the source where this happens is the InstanceTag class to which all the tag generation drills down. The method is called tag_name.

Eiten answered 7/2, 2011 at 14:17 Comment(1)
Wow thanks! I was just about to dive into the source as well. I know that it's pretty much "object[attribute]" but things are a lot more complicated when you have nested model forms - one of the reasons I was looking for such a method.Divvy
D
4
ActionView::Helpers::InstanceTag.new(
  ActiveModel::Naming.param_key(@object_in_form_for),
  :your_attribute,
  :this_param_is_ignored
).send(:tag_name)

Also there is tag_name_with_index attribute which accepts index as first parameter. Also both tag_name and tag_name_with_index have optional parameter multiple = false, which is used for arrays (just adds [] to the end of the generated name).

Finally, there are similar methods if you need id instead of name - tag_id and tag_id_with_index respectively.

Dustheap answered 19/11, 2013 at 14:26 Comment(1)
in Rails 4.2.0, it would be ActionView::Helpers::Tags::Base.new( ActiveModel::Naming.param_key(@ object_in_form_for), :your_attribute, :dummy ).send(:tag_name)Secretin
S
1

Neutrino's answer is great. Just want to add what I found, may not be exactly right. But it worked for me.

Find below method in action_view/helpers/form_tag_helper.rb

    def sanitized_object_name
      @sanitized_object_name ||= @object_name.gsub(/\]\[|[^-a-zA-Z0-9:.]/, "_").sub(/_$/, "")
    end

Hope it helps.

Sumer answered 1/5, 2011 at 20:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.