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
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
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
-----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-----
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!
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.