jawosis
2008-Jun-02 15:21 UTC
How to create multiple objects of one model in one post request?
Hi, I would apreciate to hear suggestions from you on how to create multiple objects (of the same model) with one form and post request. My concrete situation: I have a googlemaps application where users can click on a map to add a point to both the map and the html form vía javascript. When all desired points are added the user should be able to save all points within one post request to the corresponding controller/action. Do I have to write a specific action which reads the request''s different point params or is this anyhow possible with the standard create action plus a certain modification? Thanks for your contribution! Jochen --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Ajit
2008-Jun-02 19:16 UTC
Re: How to create multiple objects of one model in one post request?
can you make an array or collection while user clicks on map and when user clicks DONE you can run a loop to get all the points to save? Ajit On Jun 2, 11:21 am, jawosis <jochenke...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi, > > I would apreciate to hear suggestions from you on how to create > multiple objects (of the same model) with one form and post request. > > My concrete situation: > I have a googlemaps application where users can click on a map to add > a point to both the map and the html form vía javascript. When all > desired points are added the user should be able to save all points > within one post request to the corresponding controller/action. > Do I have to write a specific action which reads the request''s > different point params or is this anyhow possible with the standard > create action plus a certain modification? > > Thanks for your contribution! > Jochen--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Pavankumar Kulkarni
2008-Jun-02 20:22 UTC
Re: How to create multiple objects of one model in one post request?
Jochen, Building on Ajit''s solution, you can modify create action/method to act on array of points. ActiveRecord doesn''t have any method to create/save multiple objects. However if you have Map has_many Points Point belongs_to Map all you need to do is insert all those points in you map object and call a save on Map object. i.e @map = Map.new @map.points << point1 @map.points << point2 . . . @map.save Hope this helps Regards Pavan On Mon, Jun 2, 2008 at 12:16 PM, Ajit <ajitscorpio-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > can you make an array or collection while user clicks on map and when > user clicks DONE you can run a loop to get all the points to save? > Ajit > On Jun 2, 11:21 am, jawosis <jochenke...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> Hi, >> >> I would apreciate to hear suggestions from you on how to create >> multiple objects (of the same model) with one form and post request. >> >> My concrete situation: >> I have a googlemaps application where users can click on a map to add >> a point to both the map and the html form vía javascript. When all >> desired points are added the user should be able to save all points >> within one post request to the corresponding controller/action. >> Do I have to write a specific action which reads the request''s >> different point params or is this anyhow possible with the standard >> create action plus a certain modification? >> >> Thanks for your contribution! >> Jochen > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Cayce Balara
2008-Jun-03 01:23 UTC
Re: How to create multiple objects of one model in one post
Jochen Kempf wrote:> Hi, > > I would apreciate to hear suggestions from you on how to create > multiple objects (of the same model) with one form and post request. > > My concrete situation: > I have a googlemaps application where users can click on a map to add > a point to both the map and the html form v�a javascript. When all > desired points are added the user should be able to save all points > within one post request to the corresponding controller/action. > Do I have to write a specific action which reads the request''s > different point params or is this anyhow possible with the standard > create action plus a certain modification? > > Thanks for your contribution! > JochenI would think the easiest way to do this would be to create a JSON object when the page loads, update it with each click on the map and when the user submits the form, push that object through to your controller. You should be able to iterate through the JSON object from your params[] and create your points right off that using the method in the reply above mine. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Sheldon Hearn
2008-Jun-03 14:56 UTC
Re: How to create multiple objects of one model in one post request?
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On Monday 02 June 2008 17:21:18 jawosis wrote:> Do I have to write a specific action which reads the request''s > different point params or is this anyhow possible with the standard > create action plus a certain modification?Yes, but it won''t have to work very hard. The form element names should look like: points[][x] points[][y] points[][title] The trick is that the "points[]" prefix for form input names is understood by Rails to mean that you''re posting an array of things. Then, in your controller, params[:points] will be an array of hashes, each of which looks like this: { :x => 54.1, :y => 27.2, :title => "Some place" } So you can do Point.create( params[:points] ) because ActiveRecord::base.create takes an hash _or_ an array. Ciao, Sheldon. -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.6 (GNU/Linux) iD8DBQFIRVuhpGJX8XSgas0RAgZOAJ4xlcOF+u40mnEzdCNrIBU48vQRaQCeLS7H yk6GqO4+D04qBZWITPhl8YI=J+Ff -----END PGP SIGNATURE----- --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---