Hi, I''m trying to display an edit form for an object (person) with a nested object (address). Here''s the simple stuff: <%= text_field : person, :first %> <%= text_field : person, :last %> And I can display the data in the address just fine: <%= @person.address.street %> <%= @person.address.city %> But how do I render the inputs for address.street and address.city? <%= text_field_tag ''person[address][street]'', @person.address.street %> produces the following request for Person.update() {"id"=>"1", "person"=>{"first"=>"john", "last"=>"doe", "address"=> {"street"=>"Main St."}}, "submit"=>"Save"} which I thought doesn''t look so bad, but unfortunately fails with undefined method `new_record?'' for {"street"=>"Main St."}:HashWithIndifferentAccess I''m quite sure the solution is dead simple, but if anyone could help me out here, I''d appreciate it. Thanks, -Ralph.
I RTFM and found out that assigning an object to a belongs_to association does not save the object, so here''s my workaround: // edit.rhtml <%= text_field_tag ''person[address][street]'', @person.address.street %> // person_controller.rb class PersonController < ApplicationController # ... def update @person = Person.find(@params[:id]) # see http://api.rubyonrails.com/classes/ActiveRecord/ Associations/ClassMethods.html # assigning an object to a belongs_to association does not save the object, # since the foreign key field belongs on the parent. It does not save the parent either. address_attributes = @params[:person][:address]; @params[:person][:address] = nil if @person.update_attributes(@params[:person]) && @person.address.update_attributes(address_attributes) redirect_to :action => ''edit'', :id => @person else render_action ''edit'' end end end I have a gut feeling that this should not be necessary, but it works, so, um, thanks for listening. Cheers, -Ralph. On 10.06.2005, at 21:34, Ralph Pöllath wrote:> I''m trying to display an edit form for an object (person) with a > nested object (address). > > Here''s the simple stuff: > <%= text_field : person, :first %> > <%= text_field : person, :last %> > > And I can display the data in the address just fine: > <%= @person.address.street %> > <%= @person.address.city %> > > But how do I render the inputs for address.street and address.city? > > <%= text_field_tag ''person[address][street]'', > @person.address.street %> > > produces the following request for Person.update() > > {"id"=>"1", "person"=>{"first"=>"john", "last"=>"doe", "address"=> > {"street"=>"Main St."}}, "submit"=>"Save"} > > which I thought doesn''t look so bad, but unfortunately fails with > > undefined method `new_record?'' for {"street"=>"Main > St."}:HashWithIndifferentAccess > > I''m quite sure the solution is dead simple, but if anyone could > help me out here, I''d appreciate it. > > Thanks, > -Ralph.
Hi, sorry if it''s a bit late, but have you seen the composed_of method, which seems exactly what you are looking for ? http://api.rubyonrails.com/classes/ActiveRecord/Aggregations/ClassMethods.html jean On 6/12/05, Ralph Pöllath <lists-cxGDZMwwbrdg9hUCZPvPmw@public.gmane.org> wrote:> I RTFM and found out that assigning an object to a belongs_to > association does not save the object, so here''s my workaround: > > // edit.rhtml > <%= text_field_tag ''person[address][street]'', @person.address.street %> > > // person_controller.rb > class PersonController < ApplicationController > # ... > def update > @person = Person.find(@params[:id]) > > # see http://api.rubyonrails.com/classes/ActiveRecord/ > Associations/ClassMethods.html > # assigning an object to a belongs_to association does not save > the object, > # since the foreign key field belongs on the parent. It does not > save the parent either. > address_attributes = @params[:person][:address]; > @params[:person][:address] = nil > > if @person.update_attributes(@params[:person]) && > @person.address.update_attributes(address_attributes) > redirect_to :action => ''edit'', :id => @person > else > render_action ''edit'' > end > end > end > > I have a gut feeling that this should not be necessary, but it works, > so, um, thanks for listening. > > Cheers, > -Ralph. > > On 10.06.2005, at 21:34, Ralph Pöllath wrote: > > I''m trying to display an edit form for an object (person) with a > > nested object (address). > > > > Here''s the simple stuff: > > <%= text_field : person, :first %> > > <%= text_field : person, :last %> > > > > And I can display the data in the address just fine: > > <%= @person.address.street %> > > <%= @person.address.city %> > > > > But how do I render the inputs for address.street and address.city? > > > > <%= text_field_tag ''person[address][street]'', > > @person.address.street %> > > > > produces the following request for Person.update() > > > > {"id"=>"1", "person"=>{"first"=>"john", "last"=>"doe", "address"=> > > {"street"=>"Main St."}}, "submit"=>"Save"} > > > > which I thought doesn''t look so bad, but unfortunately fails with > > > > undefined method `new_record?'' for {"street"=>"Main > > St."}:HashWithIndifferentAccess > > > > I''m quite sure the solution is dead simple, but if anyone could > > help me out here, I''d appreciate it. > > > > Thanks, > > -Ralph. > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Hmm, composed_of would let me map a single table to several value objects. In my setup, the address info is stored in a separate table (because company objects also have addressses). Since I have a class and table for person and address, I think that composed_of won''t help - or does it? Thanks, -Ralph. On 12.06.2005, at 21:58, Jean Helou wrote:> Hi, > sorry if it''s a bit late, but have you seen the composed_of method, > which seems exactly what you are looking for ? > http://api.rubyonrails.com/classes/ActiveRecord/Aggregations/ > ClassMethods.html > > jean > > On 6/12/05, Ralph Pöllath <lists-cxGDZMwwbrdg9hUCZPvPmw@public.gmane.org> wrote: > >> I RTFM and found out that assigning an object to a belongs_to >> association does not save the object, so here''s my workaround: >> >> // edit.rhtml >> <%= text_field_tag ''person[address][street]'', >> @person.address.street %> >> >> // person_controller.rb >> class PersonController < ApplicationController >> # ... >> def update >> @person = Person.find(@params[:id]) >> >> # see http://api.rubyonrails.com/classes/ActiveRecord/ >> Associations/ClassMethods.html >> # assigning an object to a belongs_to association does not save >> the object, >> # since the foreign key field belongs on the parent. It does not >> save the parent either. >> address_attributes = @params[:person][:address]; >> @params[:person][:address] = nil >> >> if @person.update_attributes(@params[:person]) && >> @person.address.update_attributes(address_attributes) >> redirect_to :action => ''edit'', :id => @person >> else >> render_action ''edit'' >> end >> end >> end >> >> I have a gut feeling that this should not be necessary, but it works, >> so, um, thanks for listening. >> >> Cheers, >> -Ralph. >> >> On 10.06.2005, at 21:34, Ralph Pöllath wrote: >> >>> I''m trying to display an edit form for an object (person) with a >>> nested object (address). >>> >>> Here''s the simple stuff: >>> <%= text_field : person, :first %> >>> <%= text_field : person, :last %> >>> >>> And I can display the data in the address just fine: >>> <%= @person.address.street %> >>> <%= @person.address.city %> >>> >>> But how do I render the inputs for address.street and address.city? >>> >>> <%= text_field_tag ''person[address][street]'', >>> @person.address.street %> >>> >>> produces the following request for Person.update() >>> >>> {"id"=>"1", "person"=>{"first"=>"john", "last"=>"doe", "address"=> >>> {"street"=>"Main St."}}, "submit"=>"Save"} >>> >>> which I thought doesn''t look so bad, but unfortunately fails with >>> >>> undefined method `new_record?'' for {"street"=>"Main >>> St."}:HashWithIndifferentAccess >>> >>> I''m quite sure the solution is dead simple, but if anyone could >>> help me out here, I''d appreciate it. >>> >>> Thanks, >>> -Ralph. >>>