# Form field naming semantics question

**URL:** <https://www.ruby-forum.com/t/form-field-naming-semantics-question/95110>\
**Category:** Rails\
**Created:** [January 13, 2006, 1:17am UTC](https://www.ruby-forum.com/t/form-field-naming-semantics-question/95110 "2006-01-13T01:17:18Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![generik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/4da419/32.png) [@generik](https://www.ruby-forum.com/u/generik)\
**Post date:** [January 13, 2006, 1:17am UTC](https://www.ruby-forum.com/t/form-field-naming-semantics-question/95110/1 "2006-01-13T01:17:18Z")

</div>

Hi,

I have a question today regarding how the various form elements in  
\_form.rhtml are named.

I understand that for a database table “persons” with a column headed by  
“name”, then in \_form.rhtml it will probably be like:

Name\>  
\<%= text\_field 'person', 'name' %\>

However supposing in my Person class there is a link to another Person  
object (ie: spouse)

Is it possible to name the form fields such that the columns  
encapsulated by the spouse reference will be automatically captured as  
well?

What I’m trying to express is something like…

Spouse's Name\>  
\<%= text\_field 'person.spouse', 'name' %\>

What I’m trying to accomplish here is when you pass the whole  
params[:person] to the constructor on the Person class, it will also be  
able to set the value for the spouse Person in the Person object to the  
value contained here.

Unfortunately that’s not the correct way, but can someone please point  
me to the right way?

Thanks!

---

<div class="post-metadata">

**Author:** ![generik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/4da419/32.png) [@generik](https://www.ruby-forum.com/u/generik)\
**Post date:** [January 13, 2006, 1:57am UTC](https://www.ruby-forum.com/t/form-field-naming-semantics-question/95110/2 "2006-01-13T01:57:30Z")

</div>

Woei S. wrote:

> What I’m trying to express is something like…  
> me to the right way?  
> Model-based form helpers don’t currently work with multi-level objects.  
> However I’ve made, and am currently successfuly using, a patch that  
> enables this. With this you can write:

```
 <%= text_field 'person[spouse]', :name %>

```

The patch can be found at [http://dev.rubyonrails.org/ticket/2053](http://dev.rubyonrails.org/ticket/2053) .  
I’ve also made an unreleased extension of this that does the same  
thing for error\_messages\_on and error\_messages\_for.

This functionality seems to be requested quite often (there’s another  
post today asking about this: [Form Entity Names for Child Objects - Rails - Ruby-Forum](http://www.ruby-forum.com/topic/51548))  
so I’ll turn my research patch into a trunk candidate patch.

An alternative you can use now is:

```
<%= text_field_tag 'person[spouse][name]', @person.spouse.name %>

```

–  
We develop, watch us RoR, in numbers too big to ignore.

---

<div class="post-metadata">

**Author:** ![generik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/4da419/32.png) [@generik](https://www.ruby-forum.com/u/generik)\
**Post date:** [January 13, 2006, 8:01am UTC](https://www.ruby-forum.com/t/form-field-naming-semantics-question/95110/3 "2006-01-13T08:01:58Z")

</div>

Hi,

I’ve tried the current alternative but I got an error…

`@LineItem[Product][name]’ is not allowed as an instance variable name

Is there something else that I should be looking for? I was actually  
toying with the depot application provided with “Agile web development  
with rails” (which I guess should be a popular book here)

Thanks 🙂

Mark Reginald J. wrote:

> Woei S. wrote:
> 
> > What I’m trying to express is something like…  
> > me to the right way?  
> > Model-based form helpers don’t currently work with multi-level objects.  
> > However I’ve made, and am currently successfuly using, a patch that  
> > enables this. With this you can write:
> 
> ```
> <%= text_field 'person[spouse]', :name %>
> 
> ```
> 
> The patch can be found at [http://dev.rubyonrails.org/ticket/2053](http://dev.rubyonrails.org/ticket/2053) .  
> I’ve also made an unreleased extension of this that does the same  
> thing for error\_messages\_on and error\_messages\_for.
> 
> This functionality seems to be requested quite often (there’s another  
> post today asking about this: [Form Entity Names for Child Objects - Rails - Ruby-Forum](http://www.ruby-forum.com/topic/51548))  
> so I’ll turn my research patch into a trunk candidate patch.
> 
> An alternative you can use now is:
> 
> ```
> <%= text_field_tag 'person[spouse][name]', @person.spouse.name %>
> 
> ```
> 
> –  
> We develop, watch us RoR, in numbers too big to ignore.

---

<div class="post-metadata">

**Author:** ![generik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/4da419/32.png) [@generik](https://www.ruby-forum.com/u/generik)\
**Post date:** [January 13, 2006, 9:25am UTC](https://www.ruby-forum.com/t/form-field-naming-semantics-question/95110/4 "2006-01-13T09:25:44Z")

</div>

Woei S. wrote:

> `@LineItem[Product][name]’ is not allowed as an instance variable name

No, don’t put the ‘@’ in the string. Use either

\<%= text\_field\_tag ‘line\_item[product][name]’, @line\_item.product.name  
%\>

or

\<%= text\_field\_tag ‘line\_item[product][name]’, @line\_item.product ?  
@line\_item.product.name : nil %\>

–  
We develop, watch us RoR, in numbers too big to ignore.

---

<div class="post-metadata">

**Author:** ![generik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/4da419/32.png) [@generik](https://www.ruby-forum.com/u/generik)\
**Post date:** [January 13, 2006, 5:37pm UTC](https://www.ruby-forum.com/t/form-field-naming-semantics-question/95110/5 "2006-01-13T17:37:35Z")

</div>

On 1/13/06, Mark Reginald J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> \<%= text\_field\_tag ‘line\_item[product][name]’, @line\_item.product ? @line\_item.product.name : nil %\>

The case where I am running into this problem I am trying to use  
text\_field\_with\_auto\_complete. As I understand this workaround, it  
uses a \*\_tag helper which is for non-model attributes, but I have  
implied from your workaround that it will work with model data in this  
case. However, I can’t get the \_with\_auto\_complete to accept any  
variant that includes a multi-level object reference. Will your  
patch, if applied, allow such a thing as

\<%= text\_field\_with\_auto\_complete :part[:part\_name], :part\_name%\>

? I would love that.

Thanks!

- Ian

---

<div class="post-metadata">

**Author:** ![generik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/4da419/32.png) [@generik](https://www.ruby-forum.com/u/generik)\
**Post date:** [January 14, 2006, 4:14am UTC](https://www.ruby-forum.com/t/form-field-naming-semantics-question/95110/6 "2006-01-14T04:14:53Z")

</div>

Ian H. wrote:

> The case where I am running into this problem I am trying to use  
> text\_field\_with\_auto\_complete. As I understand this workaround, it  
> uses a \*\_tag helper which is for non-model attributes, but I have  
> implied from your workaround that it will work with model data in this  
> case. However, I can’t get the \_with\_auto\_complete to accept any  
> variant that includes a multi-level object reference. Will your  
> patch, if applied, allow such a thing as
> 
> \<%= text\_field\_with\_auto\_complete :part[:part\_name], :part\_name%\>

Yes, looking at the code for text\_field\_with\_auto\_complete I _think_  
it would work with the patch as it stands. However for your notation  
to work you’d have to mess with the Symbol class:

class Symbol  
def “#{self}[#{index}]” end  
end

With the patch as it stands you would write:

\<%= text\_field\_with\_auto\_complete ‘part[part\_name]’, :part\_name %\>

Alternatively, the patch could be changed to use Woei S.'s notation:

\<%= text\_field\_with\_auto\_complete ‘part.part\_name’, :part\_name %\>

Would this be better? The field id would be ‘part.part\_name\_part\_name’  
rather than ‘part[part\_name]\_part\_name’. This would eliminate square  
brackets from id tags, which are not allowed by the standard. But by  
the same token, square brackets aren’t allowed in name tags either,  
meaning markup being generated by Rails’ form helpers is not legal HTML!  
See [Basic HTML data types](http://www.w3.org/TR/html4/types.html#h-6.2) .

–  
We develop, watch us RoR, in numbers too big to ignore.
