olivier Hericord
2006-May-20 15:00 UTC
[Rails] HOW TO create a new record and its associated objects within the SAME PAGE
hi, can''t find any good tutorial or advice to deal with the creation of a new record and it''s associated objects within the same page. do i have to use ajax just to add custom form fields that will be handled by the controller for associated objects creation.? do i have to use ajax to add associated objects to the unsaved but allready in session parent object? what''s the best approach ? thanks
cremes.devlist@mac.com
2006-May-20 15:30 UTC
[Rails] HOW TO create a new record and its associated objects within the SAME PAGE
On May 20, 2006, at 10:00 AM, olivier Hericord wrote:> hi, > > can''t find any good tutorial or advice to deal with the creation of a > new record and it''s associated objects within the same page. > > do i have to use ajax just to add custom form fields that will be > handled by the controller for associated objects creation.? > > do i have to use ajax to add associated objects to the unsaved but > allready in session parent object? > > what''s the best approach ?I don''t think AJAX is necessary for any of this. I learned how to do it from the AWDR book, specifically pages 102 through 107. If you look at the #save_order action, it looks like: def save_order @cart = find_cart @order = Order.new(params[:order]) # <-- this is from the submitted form @order.line_items << @cart.items # <--- this automagically sets up the keys if @order.save @cart.empty! # whatever else #whatever end end So I guess the advice is to do this work in the action/method that handles the creation of the new record from a form. If you have the proper #has_many and #belongs_to directives setup in the model, AR will handle setting up all the foreign keys for you. Or maybe I''m wrong (still a newbie). cr
olivier Hericord
2006-May-20 15:41 UTC
[Rails] HOW TO create a new record and its associated objects within the SAME PAGE
on a has_many relation ajax is essential because you don''t know how many associated objects you wan to create within the parent creation. so the form has to evolve while you are adding objects...no? On 5/20/06, cremes.devlist@mac.com <cremes.devlist@mac.com> wrote:> > On May 20, 2006, at 10:00 AM, olivier Hericord wrote: > > > hi, > > > > can''t find any good tutorial or advice to deal with the creation of a > > new record and it''s associated objects within the same page. > > > > do i have to use ajax just to add custom form fields that will be > > handled by the controller for associated objects creation.? > > > > do i have to use ajax to add associated objects to the unsaved but > > allready in session parent object? > > > > what''s the best approach ? > > I don''t think AJAX is necessary for any of this. I learned how to do > it from the AWDR book, specifically pages 102 through 107. If you > look at the #save_order action, it looks like: > > def save_order > @cart = find_cart > @order = Order.new(params[:order]) # <-- this is from the > submitted form > @order.line_items << @cart.items # <--- this automagically sets up > the keys > if @order.save > @cart.empty! > # whatever > else > #whatever > end > end > > So I guess the advice is to do this work in the action/method that > handles the creation of the new record from a form. If you have the > proper #has_many and #belongs_to directives setup in the model, AR > will handle setting up all the foreign keys for you. > > Or maybe I''m wrong (still a newbie). > > cr > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Kevin Olbrich
2006-May-20 16:00 UTC
Re: [Rails] HOW TO create a new record and its associated objects within the SAME PAGE
On Saturday, May 20, 2006, at 5:41 PM, olivier Hericord wrote:>on a has_many relation ajax is essential because you don''t know how >many associated objects you wan to create within the parent creation. > >so the form has to evolve while you are adding objects...no? > >On 5/20/06, cremes.devlist@mac.com <cremes.devlist@mac.com> wrote: >> >> On May 20, 2006, at 10:00 AM, olivier Hericord wrote: >> >> > hi, >> > >> > can''t find any good tutorial or advice to deal with the creation of a >> > new record and it''s associated objects within the same page. >> > >> > do i have to use ajax just to add custom form fields that will be >> > handled by the controller for associated objects creation.? >> > >> > do i have to use ajax to add associated objects to the unsaved but >> > allready in session parent object? >> > >> > what''s the best approach ? >> >> I don''t think AJAX is necessary for any of this. I learned how to do >> it from the AWDR book, specifically pages 102 through 107. If you >> look at the #save_order action, it looks like: >> >> def save_order >> @cart = find_cart >> @order = Order.new(params[:order]) # <-- this is from the >> submitted form >> @order.line_items << @cart.items # <--- this automagically sets up >> the keys >> if @order.save >> @cart.empty! >> # whatever >> else >> #whatever >> end >> end >> >> So I guess the advice is to do this work in the action/method that >> handles the creation of the new record from a form. If you have the >> proper #has_many and #belongs_to directives setup in the model, AR >> will handle setting up all the foreign keys for you. >> >> Or maybe I''m wrong (still a newbie). >> >> cr >> >> _______________________________________________ >> Rails mailing list >> Rails@lists.rubyonrails.org >> http://lists.rubyonrails.org/mailman/listinfo/rails >> >_______________________________________________ >Rails mailing list >Rails@lists.rubyonrails.org >http://lists.rubyonrails.org/mailman/listinfo/railsSo far as I can tell, the only way to avoid the use of AJAX in a form for an object that may associate with many other items is to force the user to enter the associated objects before calling the form. In some cases this may not be a problem, but in many it is. For example... If you have a Person model that has_many PhoneNumbers, it is fairly impractical to expect the user to enter the phone numbers before entering the person''s information. It is possible to set up the form to accept a fixed number of sub objects that will be associated. Following from the previous example, you would need to set up form fields for 10 new phone numbers, and then hope that was enough. --- One approach that may work would be to store the main object and it''s associated objects in the session and then save the whole lot when the form is submitted. _Kevin -- Posted with http://DevLists.com. Sign up and save your mailbox.
Steven Hansen
2006-May-20 16:08 UTC
[Rails] HOW TO create a new record and its associated objects within the SAME PAGE
<snip> One approach that may work would be to store the main object and it''s associated objects in the session and then save the whole lot when the form is submitted. </snip> I''ve used this method quit a few times. Even if you decide to use AJAX to associate the children with the parent, you''ll need to put the parent object into a session first. -Steven Kevin Olbrich wrote:>On Saturday, May 20, 2006, at 5:41 PM, olivier Hericord wrote: > > >>on a has_many relation ajax is essential because you don''t know how >>many associated objects you wan to create within the parent creation. >> >>so the form has to evolve while you are adding objects...no? >> >>On 5/20/06, cremes.devlist@mac.com <cremes.devlist@mac.com> wrote: >> >> >>>On May 20, 2006, at 10:00 AM, olivier Hericord wrote: >>> >>> >>> >>>>hi, >>>> >>>>can''t find any good tutorial or advice to deal with the creation of a >>>>new record and it''s associated objects within the same page. >>>> >>>>do i have to use ajax just to add custom form fields that will be >>>>handled by the controller for associated objects creation.? >>>> >>>>do i have to use ajax to add associated objects to the unsaved but >>>>allready in session parent object? >>>> >>>>what''s the best approach ? >>>> >>>> >>>I don''t think AJAX is necessary for any of this. I learned how to do >>>it from the AWDR book, specifically pages 102 through 107. If you >>>look at the #save_order action, it looks like: >>> >>>def save_order >>> @cart = find_cart >>> @order = Order.new(params[:order]) # <-- this is from the >>>submitted form >>> @order.line_items << @cart.items # <--- this automagically sets up >>>the keys >>> if @order.save >>> @cart.empty! >>> # whatever >>> else >>> #whatever >>> end >>>end >>> >>>So I guess the advice is to do this work in the action/method that >>>handles the creation of the new record from a form. If you have the >>>proper #has_many and #belongs_to directives setup in the model, AR >>>will handle setting up all the foreign keys for you. >>> >>>Or maybe I''m wrong (still a newbie). >>> >>>cr >>> >>>_______________________________________________ >>>Rails mailing list >>>Rails@lists.rubyonrails.org >>>http://lists.rubyonrails.org/mailman/listinfo/rails >>> >>> >>> >>_______________________________________________ >>Rails mailing list >>Rails@lists.rubyonrails.org >>http://lists.rubyonrails.org/mailman/listinfo/rails >> >> > >So far as I can tell, the only way to avoid the use of AJAX in a form >for an object that may associate with many other items is to force the >user to enter the associated objects before calling the form. In some >cases this may not be a problem, but in many it is. For example... > >If you have a Person model that has_many PhoneNumbers, it is fairly >impractical to expect the user to enter the phone numbers before >entering the person''s information. > >It is possible to set up the form to accept a fixed number of sub >objects that will be associated. Following from the previous example, >you would need to set up form fields for 10 new phone numbers, and then >hope that was enough. > >--- > >One approach that may work would be to store the main object and it''s >associated objects in the session and then save the whole lot when the >form is submitted. > >_Kevin > > >
olivier Hericord
2006-May-21 00:20 UTC
[Rails] HOW TO create a new record and its associated objects within the SAME PAGE
i decided to put the unsaved parent object in sessiion .... that allows me to create via ajax associated object that are not lost if the parent saving raise an error... let''s take the example of the address book. the main form is the person form. so we have form fields like this one: <input id="person_first_name" name="person[first_name]" size="30" type="text" /> it would be cool to have an helper that can generate a field like this one: <input id="person_address_street" name="person[address_street][]" size="30" type="text" /> automagically the application will understand that it''s the array containing the street field of all the addresses attached to the person object seems crazy? On 5/20/06, Steven Hansen <runner@berkeley.edu> wrote:> > <snip> > > One approach that may work would be to store the main object and it''s > associated objects in the session and then save the whole lot when the > form is submitted. > > </snip> > > I''ve used this method quit a few times. Even if you decide to use AJAX > to associate the children with the parent, you''ll need to put the parent > object into a session first. > > -Steven > > > Kevin Olbrich wrote: > > >On Saturday, May 20, 2006, at 5:41 PM, olivier Hericord wrote: > > > > > >>on a has_many relation ajax is essential because you don''t know how > >>many associated objects you wan to create within the parent creation. > >> > >>so the form has to evolve while you are adding objects...no? > >> > >>On 5/20/06, cremes.devlist@mac.com <cremes.devlist@mac.com> wrote: > >> > >> > >>>On May 20, 2006, at 10:00 AM, olivier Hericord wrote: > >>> > >>> > >>> > >>>>hi, > >>>> > >>>>can''t find any good tutorial or advice to deal with the creation of a > >>>>new record and it''s associated objects within the same page. > >>>> > >>>>do i have to use ajax just to add custom form fields that will be > >>>>handled by the controller for associated objects creation.? > >>>> > >>>>do i have to use ajax to add associated objects to the unsaved but > >>>>allready in session parent object? > >>>> > >>>>what''s the best approach ? > >>>> > >>>> > >>>I don''t think AJAX is necessary for any of this. I learned how to do > >>>it from the AWDR book, specifically pages 102 through 107. If you > >>>look at the #save_order action, it looks like: > >>> > >>>def save_order > >>> @cart = find_cart > >>> @order = Order.new(params[:order]) # <-- this is from the > >>>submitted form > >>> @order.line_items << @cart.items # <--- this automagically sets up > >>>the keys > >>> if @order.save > >>> @cart.empty! > >>> # whatever > >>> else > >>> #whatever > >>> end > >>>end > >>> > >>>So I guess the advice is to do this work in the action/method that > >>>handles the creation of the new record from a form. If you have the > >>>proper #has_many and #belongs_to directives setup in the model, AR > >>>will handle setting up all the foreign keys for you. > >>> > >>>Or maybe I''m wrong (still a newbie). > >>> > >>>cr > >>> > >>>_______________________________________________ > >>>Rails mailing list > >>>Rails@lists.rubyonrails.org > >>>http://lists.rubyonrails.org/mailman/listinfo/rails > >>> > >>> > >>> > >>_______________________________________________ > >>Rails mailing list > >>Rails@lists.rubyonrails.org > >>http://lists.rubyonrails.org/mailman/listinfo/rails > >> > >> > > > >So far as I can tell, the only way to avoid the use of AJAX in a form > >for an object that may associate with many other items is to force the > >user to enter the associated objects before calling the form. In some > >cases this may not be a problem, but in many it is. For example... > > > >If you have a Person model that has_many PhoneNumbers, it is fairly > >impractical to expect the user to enter the phone numbers before > >entering the person''s information. > > > >It is possible to set up the form to accept a fixed number of sub > >objects that will be associated. Following from the previous example, > >you would need to set up form fields for 10 new phone numbers, and then > >hope that was enough. > > > >--- > > > >One approach that may work would be to store the main object and it''s > >associated objects in the session and then save the whole lot when the > >form is submitted. > > > >_Kevin > > > > > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
olivier Hericord
2006-May-21 00:24 UTC
[Rails] HOW TO create a new record and its associated objects within the SAME PAGE
with ajax it would be possible to generate for the first attached address: <input name="person[address_street][1]" size="30" type="text" /> <input name="person[address_city][1]" size="30" type="text" /> <input name="person[address_country][1]" size="30" type="text" /> then for the second attached address: <input name="person[address_street][2]" size="30" type="text" /> <input name="person[address_city][2]" size="30" type="text" /> <input name="person[address_country][2]" size="30" type="text" /> and so on.... On 5/21/06, olivier Hericord <olivier.hericord.lists@gmail.com> wrote:> i decided to put the unsaved parent object in sessiion .... that > allows me to create via ajax associated object that are not lost if > the parent saving raise an error... > > let''s take the example of the address book. > > the main form is the person form. > so we have form fields like this one: > <input id="person_first_name" name="person[first_name]" size="30" type="text" /> > > > > it would be cool to have an helper that can generate a field like this one: > <input id="person_address_street" name="person[address_street][]" > size="30" type="text" /> > > automagically the application will understand that it''s the array > containing the street field of all the addresses attached to the > person object > > seems crazy? > > > On 5/20/06, Steven Hansen <runner@berkeley.edu> wrote: > > > > <snip> > > > > One approach that may work would be to store the main object and it''s > > associated objects in the session and then save the whole lot when the > > form is submitted. > > > > </snip> > > > > I''ve used this method quit a few times. Even if you decide to use AJAX > > to associate the children with the parent, you''ll need to put the parent > > object into a session first. > > > > -Steven > > > > > > Kevin Olbrich wrote: > > > > >On Saturday, May 20, 2006, at 5:41 PM, olivier Hericord wrote: > > > > > > > > >>on a has_many relation ajax is essential because you don''t know how > > >>many associated objects you wan to create within the parent creation. > > >> > > >>so the form has to evolve while you are adding objects...no? > > >> > > >>On 5/20/06, cremes.devlist@mac.com <cremes.devlist@mac.com> wrote: > > >> > > >> > > >>>On May 20, 2006, at 10:00 AM, olivier Hericord wrote: > > >>> > > >>> > > >>> > > >>>>hi, > > >>>> > > >>>>can''t find any good tutorial or advice to deal with the creation of a > > >>>>new record and it''s associated objects within the same page. > > >>>> > > >>>>do i have to use ajax just to add custom form fields that will be > > >>>>handled by the controller for associated objects creation.? > > >>>> > > >>>>do i have to use ajax to add associated objects to the unsaved but > > >>>>allready in session parent object? > > >>>> > > >>>>what''s the best approach ? > > >>>> > > >>>> > > >>>I don''t think AJAX is necessary for any of this. I learned how to do > > >>>it from the AWDR book, specifically pages 102 through 107. If you > > >>>look at the #save_order action, it looks like: > > >>> > > >>>def save_order > > >>> @cart = find_cart > > >>> @order = Order.new(params[:order]) # <-- this is from the > > >>>submitted form > > >>> @order.line_items << @cart.items # <--- this automagically sets up > > >>>the keys > > >>> if @order.save > > >>> @cart.empty! > > >>> # whatever > > >>> else > > >>> #whatever > > >>> end > > >>>end > > >>> > > >>>So I guess the advice is to do this work in the action/method that > > >>>handles the creation of the new record from a form. If you have the > > >>>proper #has_many and #belongs_to directives setup in the model, AR > > >>>will handle setting up all the foreign keys for you. > > >>> > > >>>Or maybe I''m wrong (still a newbie). > > >>> > > >>>cr > > >>> > > >>>_______________________________________________ > > >>>Rails mailing list > > >>>Rails@lists.rubyonrails.org > > >>>http://lists.rubyonrails.org/mailman/listinfo/rails > > >>> > > >>> > > >>> > > >>_______________________________________________ > > >>Rails mailing list > > >>Rails@lists.rubyonrails.org > > >>http://lists.rubyonrails.org/mailman/listinfo/rails > > >> > > >> > > > > > >So far as I can tell, the only way to avoid the use of AJAX in a form > > >for an object that may associate with many other items is to force the > > >user to enter the associated objects before calling the form. In some > > >cases this may not be a problem, but in many it is. For example... > > > > > >If you have a Person model that has_many PhoneNumbers, it is fairly > > >impractical to expect the user to enter the phone numbers before > > >entering the person''s information. > > > > > >It is possible to set up the form to accept a fixed number of sub > > >objects that will be associated. Following from the previous example, > > >you would need to set up form fields for 10 new phone numbers, and then > > >hope that was enough. > > > > > >--- > > > > > >One approach that may work would be to store the main object and it''s > > >associated objects in the session and then save the whole lot when the > > >form is submitted. > > > > > >_Kevin > > > > > > > > > > > _______________________________________________ > > Rails mailing list > > Rails@lists.rubyonrails.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > >
Zack Ham
2006-May-21 01:04 UTC
[Rails] HOW TO create a new record and its associated objects within the SAME PAGE
You definitely do not need to use asychronous server calls for this at all. Just use the DOM methods to create the form fields on the fly. Here is a page that has a quick rundown of the methods available to you: http://www.webreference.com/js/column43/methods.html Zack Ham On 5/20/06, olivier Hericord <olivier.hericord.lists@gmail.com> wrote:> > with ajax it would be possible to generate for the first attached address: > <input name="person[address_street][1]" size="30" type="text" /> > <input name="person[address_city][1]" size="30" type="text" /> > <input name="person[address_country][1]" size="30" type="text" /> > > then for the second attached address: > <input name="person[address_street][2]" size="30" type="text" /> > <input name="person[address_city][2]" size="30" type="text" /> > <input name="person[address_country][2]" size="30" type="text" /> > > and so on.... > > > > On 5/21/06, olivier Hericord <olivier.hericord.lists@gmail.com> wrote: > > i decided to put the unsaved parent object in sessiion .... that > > allows me to create via ajax associated object that are not lost if > > the parent saving raise an error... > > > > let''s take the example of the address book. > > > > the main form is the person form. > > so we have form fields like this one: > > <input id="person_first_name" name="person[first_name]" size="30" > type="text" /> > > > > > > > > it would be cool to have an helper that can generate a field like this > one: > > <input id="person_address_street" name="person[address_street][]" > > size="30" type="text" /> > > > > automagically the application will understand that it''s the array > > containing the street field of all the addresses attached to the > > person object > > > > seems crazy? > > > > > > On 5/20/06, Steven Hansen <runner@berkeley.edu> wrote: > > > > > > <snip> > > > > > > One approach that may work would be to store the main object and it''s > > > associated objects in the session and then save the whole lot when the > > > form is submitted. > > > > > > </snip> > > > > > > I''ve used this method quit a few times. Even if you decide to use > AJAX > > > to associate the children with the parent, you''ll need to put the > parent > > > object into a session first. > > > > > > -Steven > > > > > > > > > Kevin Olbrich wrote: > > > > > > >On Saturday, May 20, 2006, at 5:41 PM, olivier Hericord wrote: > > > > > > > > > > > >>on a has_many relation ajax is essential because you don''t know how > > > >>many associated objects you wan to create within the parent > creation. > > > >> > > > >>so the form has to evolve while you are adding objects...no? > > > >> > > > >>On 5/20/06, cremes.devlist@mac.com <cremes.devlist@mac.com> wrote: > > > >> > > > >> > > > >>>On May 20, 2006, at 10:00 AM, olivier Hericord wrote: > > > >>> > > > >>> > > > >>> > > > >>>>hi, > > > >>>> > > > >>>>can''t find any good tutorial or advice to deal with the creation > of a > > > >>>>new record and it''s associated objects within the same page. > > > >>>> > > > >>>>do i have to use ajax just to add custom form fields that will be > > > >>>>handled by the controller for associated objects creation.? > > > >>>> > > > >>>>do i have to use ajax to add associated objects to the unsaved but > > > >>>>allready in session parent object? > > > >>>> > > > >>>>what''s the best approach ? > > > >>>> > > > >>>> > > > >>>I don''t think AJAX is necessary for any of this. I learned how to > do > > > >>>it from the AWDR book, specifically pages 102 through 107. If you > > > >>>look at the #save_order action, it looks like: > > > >>> > > > >>>def save_order > > > >>> @cart = find_cart > > > >>> @order = Order.new(params[:order]) # <-- this is from the > > > >>>submitted form > > > >>> @order.line_items << @cart.items # <--- this automagically sets > up > > > >>>the keys > > > >>> if @order.save > > > >>> @cart.empty! > > > >>> # whatever > > > >>> else > > > >>> #whatever > > > >>> end > > > >>>end > > > >>> > > > >>>So I guess the advice is to do this work in the action/method that > > > >>>handles the creation of the new record from a form. If you have the > > > >>>proper #has_many and #belongs_to directives setup in the model, AR > > > >>>will handle setting up all the foreign keys for you. > > > >>> > > > >>>Or maybe I''m wrong (still a newbie). > > > >>> > > > >>>cr > > > >>> > > > >>>_______________________________________________ > > > >>>Rails mailing list > > > >>>Rails@lists.rubyonrails.org > > > >>>http://lists.rubyonrails.org/mailman/listinfo/rails > > > >>> > > > >>> > > > >>> > > > >>_______________________________________________ > > > >>Rails mailing list > > > >>Rails@lists.rubyonrails.org > > > >>http://lists.rubyonrails.org/mailman/listinfo/rails > > > >> > > > >> > > > > > > > >So far as I can tell, the only way to avoid the use of AJAX in a form > > > >for an object that may associate with many other items is to force > the > > > >user to enter the associated objects before calling the form. In > some > > > >cases this may not be a problem, but in many it is. For example... > > > > > > > >If you have a Person model that has_many PhoneNumbers, it is fairly > > > >impractical to expect the user to enter the phone numbers before > > > >entering the person''s information. > > > > > > > >It is possible to set up the form to accept a fixed number of sub > > > >objects that will be associated. Following from the previous > example, > > > >you would need to set up form fields for 10 new phone numbers, and > then > > > >hope that was enough. > > > > > > > >--- > > > > > > > >One approach that may work would be to store the main object and it''s > > > >associated objects in the session and then save the whole lot when > the > > > >form is submitted. > > > > > > > >_Kevin > > > > > > > > > > > > > > > _______________________________________________ > > > Rails mailing list > > > Rails@lists.rubyonrails.org > > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060521/c2666b38/attachment.html
olivier Hericord
2006-May-21 20:20 UTC
[Rails] HOW TO create a new record and its associated objects within the SAME PAGE
yes but ajax allows me to achieve validations. On 5/21/06, Zack Ham <zackham@gmail.com> wrote:> You definitely do not need to use asychronous server calls for this at all. > Just use the DOM methods to create the form fields on the fly. Here is a > page that has a quick rundown of the methods available to you: > http://www.webreference.com/js/column43/methods.html > > > Zack Ham > > > On 5/20/06, olivier Hericord < > olivier.hericord.lists@gmail.com> wrote: > > with ajax it would be possible to generate for the first attached address: > > <input name="person[address_street][1]" size="30" > type="text" /> > > <input name="person[address_city][1]" size="30" > type="text" /> > > <input name="person[address_country][1]" size="30" > type="text" /> > > > > then for the second attached address: > > <input name="person[address_street][2]" size="30" > type="text" /> > > <input name="person[address_city][2]" size="30" > type="text" /> > > <input name="person[address_country][2]" size="30" > type="text" /> > > > > and so on.... > > > > > > > > On 5/21/06, olivier Hericord < > olivier.hericord.lists@gmail.com> wrote: > > > i decided to put the unsaved parent object in sessiion .... that > > > allows me to create via ajax associated object that are not lost if > > > the parent saving raise an error... > > > > > > let''s take the example of the address book. > > > > > > the main form is the person form. > > > so we have form fields like this one: > > > <input id="person_first_name" name="person[first_name]" size="30" > type="text" /> > > > > > > > > > > > > it would be cool to have an helper that can generate a field like this > one: > > > <input id="person_address_street" name="person[address_street][]" > > > size="30" type="text" /> > > > > > > automagically the application will understand that it''s the array > > > containing the street field of all the addresses attached to the > > > person object > > > > > > seems crazy? > > > > > > > > > On 5/20/06, Steven Hansen <runner@berkeley.edu> wrote: > > > > > > > > <snip> > > > > > > > > One approach that may work would be to store the main object and it''s > > > > associated objects in the session and then save the whole lot when the > > > > form is submitted. > > > > > > > > </snip> > > > > > > > > I''ve used this method quit a few times. Even if you decide to use > AJAX > > > > to associate the children with the parent, you''ll need to put the > parent > > > > object into a session first. > > > > > > > > -Steven > > > > > > > > > > > > Kevin Olbrich wrote: > > > > > > > > >On Saturday, May 20, 2006, at 5:41 PM, olivier Hericord wrote: > > > > > > > > > > > > > > >>on a has_many relation ajax is essential because you don''t know how > > > > >>many associated objects you wan to create within the parent > creation. > > > > >> > > > > >>so the form has to evolve while you are adding objects...no? > > > > >> > > > > >>On 5/20/06, cremes.devlist@mac.com <cremes.devlist@mac.com> wrote: > > > > >> > > > > >> > > > > >>>On May 20, 2006, at 10:00 AM, olivier Hericord wrote: > > > > >>> > > > > >>> > > > > >>> > > > > >>>>hi, > > > > >>>> > > > > >>>>can''t find any good tutorial or advice to deal with the creation > of a > > > > >>>>new record and it''s associated objects within the same page. > > > > >>>> > > > > >>>>do i have to use ajax just to add custom form fields that will be > > > > >>>>handled by the controller for associated objects creation.? > > > > >>>> > > > > >>>>do i have to use ajax to add associated objects to the unsaved but > > > > >>>>allready in session parent object? > > > > >>>> > > > > >>>>what''s the best approach ? > > > > >>>> > > > > >>>> > > > > >>>I don''t think AJAX is necessary for any of this. I learned how to > do > > > > >>>it from the AWDR book, specifically pages 102 through 107. If you > > > > >>>look at the #save_order action, it looks like: > > > > >>> > > > > >>>def save_order > > > > >>> @cart = find_cart > > > > >>> @order = Order.new(params[:order]) # <-- this is from the > > > > >>>submitted form > > > > >>> @order.line_items << @cart.items # <--- this automagically sets > up > > > > >>>the keys > > > > >>> if @order.save > > > > >>> @cart.empty! > > > > >>> # whatever > > > > >>> else > > > > >>> #whatever > > > > >>> end > > > > >>>end > > > > >>> > > > > >>>So I guess the advice is to do this work in the action/method that > > > > >>>handles the creation of the new record from a form. If you have the > > > > >>>proper #has_many and #belongs_to directives setup in the model, AR > > > > >>>will handle setting up all the foreign keys for you. > > > > >>> > > > > >>>Or maybe I''m wrong (still a newbie). > > > > >>> > > > > >>>cr > > > > >>> > > > > >>>_______________________________________________ > > > > >>>Rails mailing list > > > > >>> Rails@lists.rubyonrails.org > > > > > >>>http://lists.rubyonrails.org/mailman/listinfo/rails > > > > >>> > > > > >>> > > > > >>> > > > > >>_______________________________________________ > > > > >>Rails mailing list > > > > >> Rails@lists.rubyonrails.org > > > > > >>http://lists.rubyonrails.org/mailman/listinfo/rails > > > > >> > > > > >> > > > > > > > > > >So far as I can tell, the only way to avoid the use of AJAX in a form > > > > >for an object that may associate with many other items is to force > the > > > > >user to enter the associated objects before calling the form. In > some > > > > >cases this may not be a problem, but in many it is. For example... > > > > > > > > > >If you have a Person model that has_many PhoneNumbers, it is fairly > > > > >impractical to expect the user to enter the phone numbers before > > > > >entering the person''s information. > > > > > > > > > >It is possible to set up the form to accept a fixed number of sub > > > > >objects that will be associated. Following from the previous > example, > > > > >you would need to set up form fields for 10 new phone numbers, and > then > > > > >hope that was enough. > > > > > > > > > >--- > > > > > > > > > >One approach that may work would be to store the main object and it''s > > > > >associated objects in the session and then save the whole lot when > the > > > > >form is submitted. > > > > > > > > > >_Kevin > > > > > > > > > > > > > > > > > > > _______________________________________________ > > > > Rails mailing list > > > > Rails@lists.rubyonrails.org > > > > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > > > > > _______________________________________________ > > Rails mailing list > > Rails@lists.rubyonrails.org > > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >
Brian Hogan
2006-May-22 16:12 UTC
[Rails] HOW TO create a new record and its associated objects within the SAME PAGE
In almost any case, an experienced web developer can accomplish what you''re attempting without using AJAX. Whether you ask for the number of associated items at once or walk the user through a wizard-like flow (add another item or finish). Validations can be acheived without using AJAX. Just retrieve them all at once and display them. If you plan to use AJAX in *any* application, you should be aware of the implications that its use can have for your users. http://www.standards-schmandards.com/index.php?2005/03/01/16-ajax-and-accessibility and also http://www.sitepoint.com/article/ajax-screenreaders-work On 5/21/06, olivier Hericord <olivier.hericord.lists@gmail.com> wrote:> yes but ajax allows me to achieve validations. > > On 5/21/06, Zack Ham <zackham@gmail.com> wrote: > > You definitely do not need to use asychronous server calls for this at all. > > Just use the DOM methods to create the form fields on the fly. Here is a > > page that has a quick rundown of the methods available to you: > > http://www.webreference.com/js/column43/methods.html > > > > > > Zack Ham > > > > > > On 5/20/06, olivier Hericord < > > olivier.hericord.lists@gmail.com> wrote: > > > with ajax it would be possible to generate for the first attached address: > > > <input name="person[address_street][1]" size="30" > > type="text" /> > > > <input name="person[address_city][1]" size="30" > > type="text" /> > > > <input name="person[address_country][1]" size="30" > > type="text" /> > > > > > > then for the second attached address: > > > <input name="person[address_street][2]" size="30" > > type="text" /> > > > <input name="person[address_city][2]" size="30" > > type="text" /> > > > <input name="person[address_country][2]" size="30" > > type="text" /> > > > > > > and so on.... > > > > > > > > > > > > On 5/21/06, olivier Hericord < > > olivier.hericord.lists@gmail.com> wrote: > > > > i decided to put the unsaved parent object in sessiion .... that > > > > allows me to create via ajax associated object that are not lost if > > > > the parent saving raise an error... > > > > > > > > let''s take the example of the address book. > > > > > > > > the main form is the person form. > > > > so we have form fields like this one: > > > > <input id="person_first_name" name="person[first_name]" size="30" > > type="text" /> > > > > > > > > > > > > > > > > it would be cool to have an helper that can generate a field like this > > one: > > > > <input id="person_address_street" name="person[address_street][]" > > > > size="30" type="text" /> > > > > > > > > automagically the application will understand that it''s the array > > > > containing the street field of all the addresses attached to the > > > > person object > > > > > > > > seems crazy? > > > > > > > > > > > > On 5/20/06, Steven Hansen <runner@berkeley.edu> wrote: > > > > > > > > > > <snip> > > > > > > > > > > One approach that may work would be to store the main object and it''s > > > > > associated objects in the session and then save the whole lot when the > > > > > form is submitted. > > > > > > > > > > </snip> > > > > > > > > > > I''ve used this method quit a few times. Even if you decide to use > > AJAX > > > > > to associate the children with the parent, you''ll need to put the > > parent > > > > > object into a session first. > > > > > > > > > > -Steven > > > > > > > > > > > > > > > Kevin Olbrich wrote: > > > > > > > > > > >On Saturday, May 20, 2006, at 5:41 PM, olivier Hericord wrote: > > > > > > > > > > > > > > > > > >>on a has_many relation ajax is essential because you don''t know how > > > > > >>many associated objects you wan to create within the parent > > creation. > > > > > >> > > > > > >>so the form has to evolve while you are adding objects...no? > > > > > >> > > > > > >>On 5/20/06, cremes.devlist@mac.com <cremes.devlist@mac.com> wrote: > > > > > >> > > > > > >> > > > > > >>>On May 20, 2006, at 10:00 AM, olivier Hericord wrote: > > > > > >>> > > > > > >>> > > > > > >>> > > > > > >>>>hi, > > > > > >>>> > > > > > >>>>can''t find any good tutorial or advice to deal with the creation > > of a > > > > > >>>>new record and it''s associated objects within the same page. > > > > > >>>> > > > > > >>>>do i have to use ajax just to add custom form fields that will be > > > > > >>>>handled by the controller for associated objects creation.? > > > > > >>>> > > > > > >>>>do i have to use ajax to add associated objects to the unsaved but > > > > > >>>>allready in session parent object? > > > > > >>>> > > > > > >>>>what''s the best approach ? > > > > > >>>> > > > > > >>>> > > > > > >>>I don''t think AJAX is necessary for any of this. I learned how to > > do > > > > > >>>it from the AWDR book, specifically pages 102 through 107. If you > > > > > >>>look at the #save_order action, it looks like: > > > > > >>> > > > > > >>>def save_order > > > > > >>> @cart = find_cart > > > > > >>> @order = Order.new(params[:order]) # <-- this is from the > > > > > >>>submitted form > > > > > >>> @order.line_items << @cart.items # <--- this automagically sets > > up > > > > > >>>the keys > > > > > >>> if @order.save > > > > > >>> @cart.empty! > > > > > >>> # whatever > > > > > >>> else > > > > > >>> #whatever > > > > > >>> end > > > > > >>>end > > > > > >>> > > > > > >>>So I guess the advice is to do this work in the action/method that > > > > > >>>handles the creation of the new record from a form. If you have the > > > > > >>>proper #has_many and #belongs_to directives setup in the model, AR > > > > > >>>will handle setting up all the foreign keys for you. > > > > > >>> > > > > > >>>Or maybe I''m wrong (still a newbie). > > > > > >>> > > > > > >>>cr > > > > > >>> > > > > > >>>_______________________________________________ > > > > > >>>Rails mailing list > > > > > >>> Rails@lists.rubyonrails.org > > > > > > > >>>http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > >>> > > > > > >>> > > > > > >>> > > > > > >>_______________________________________________ > > > > > >>Rails mailing list > > > > > >> Rails@lists.rubyonrails.org > > > > > > > >>http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > >> > > > > > >> > > > > > > > > > > > >So far as I can tell, the only way to avoid the use of AJAX in a form > > > > > >for an object that may associate with many other items is to force > > the > > > > > >user to enter the associated objects before calling the form. In > > some > > > > > >cases this may not be a problem, but in many it is. For example... > > > > > > > > > > > >If you have a Person model that has_many PhoneNumbers, it is fairly > > > > > >impractical to expect the user to enter the phone numbers before > > > > > >entering the person''s information. > > > > > > > > > > > >It is possible to set up the form to accept a fixed number of sub > > > > > >objects that will be associated. Following from the previous > > example, > > > > > >you would need to set up form fields for 10 new phone numbers, and > > then > > > > > >hope that was enough. > > > > > > > > > > > >--- > > > > > > > > > > > >One approach that may work would be to store the main object and it''s > > > > > >associated objects in the session and then save the whole lot when > > the > > > > > >form is submitted. > > > > > > > > > > > >_Kevin > > > > > > > > > > > > > > > > > > > > > > > _______________________________________________ > > > > > Rails mailing list > > > > > Rails@lists.rubyonrails.org > > > > > > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > > > > > > > > _______________________________________________ > > > Rails mailing list > > > Rails@lists.rubyonrails.org > > > > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > > > > > _______________________________________________ > > Rails mailing list > > Rails@lists.rubyonrails.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Curtis
2006-May-22 19:12 UTC
[Rails] HOW TO create a new record and its associated objects within the SAME PAGE
On 5/22/06, Brian Hogan <bphogan@gmail.com> wrote:> In almost any case, an experienced web developer can accomplish what > you''re attempting without using AJAX. Whether you ask for the number > of associated items at once or walk the user through a wizard-like > flow (add another item or finish).Yupyup... I''d go with a wizard interface. It''s quick, easy, and a UI pattern that users are familiar with. And it doesn''t rely on AJAX for validations, you can just use standard validations at the proper places.