Form field naming semantics question

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!

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 .
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)
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.

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 :slight_smile:

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 .
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)
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.

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.

On 1/13/06, Mark Reginald J. [email protected] 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

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 .


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