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