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! -- Posted via http://www.ruby-forum.com/.
Mark Reginald James
2006-Jan-13 00:54 UTC
[Rails] Re: Form field naming semantics question
Woei Shyang wrote:> <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?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.
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.-- Posted via http://www.ruby-forum.com/.
Mark Reginald James
2006-Jan-13 08:21 UTC
[Rails] Re: Form field naming semantics question
Woei Shyang wrote:> `@LineItem[Product][name]'' is not allowed as an instance variable nameNo, 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 James <mrj@bigpond.net.au> wrote:> 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 %> >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
Mark Reginald James
2006-Jan-14 03:13 UTC
[Rails] Re: Form field naming semantics question
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.