Forum: Ruby on Rails Form field naming semantics question

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Fcdafe495dccc04d27f6dbbcdf54aecc?d=identicon&s=25 Woei Shyang (generik)
on 2006-01-13 01:17
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:

<p><label for="person_name">Name><br/>
<%= text_field 'person', 'name'  %></p>

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

<p><label for="person.spouse_name">Spouse's Name><br/>
<%= text_field 'person.spouse', 'name'  %></p>

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!
C64e63b70be7dfed8b0742540b8b27e5?d=identicon&s=25 Mark Reginald James (Guest)
on 2006-01-13 01:57
(Received via mailing list)
Woei Shyang 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: 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.
Fcdafe495dccc04d27f6dbbcdf54aecc?d=identicon&s=25 Woei Shyang (generik)
on 2006-01-13 08:01
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 James wrote:
> Woei Shyang 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: 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.
C64e63b70be7dfed8b0742540b8b27e5?d=identicon&s=25 Mark Reginald James (Guest)
on 2006-01-13 09:25
(Received via mailing list)
Woei Shyang 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.
8ed6d55dddf47e0974bac833f08e4390?d=identicon&s=25 Ian Harding (Guest)
on 2006-01-13 17:37
(Received via mailing list)
On 1/13/06, Mark Reginald James <mrj@bigpond.net.au> 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
C64e63b70be7dfed8b0742540b8b27e5?d=identicon&s=25 Mark Reginald James (Guest)
on 2006-01-14 04:14
(Received via mailing list)
Ian Harding 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 [](index) "#{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 Shyang'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 http://www.w3.org/TR/html4/types.html#h-6.2 .

--
We develop, watch us RoR, in numbers too big to ignore.
This topic is locked and can not be replied to.