Dear All,
On p. 356 of Agile Web D. with Rails, there is a method
describing how to edit multiple objects from the same model on one
form:
Here’s the text:
Forms Containing Collections If you need to edit multiple objects from the same model on one form, add open and closed brackets to the name of the instance variable you pass to the form helpers. This tells Rails to include the object?s id as part of the field name. For example, the following template lets a user alter one or more image URLs associated with a list of products.<%= start_form_tag %>
<% for @product in @products %>
<%= text_field(“product[]”, ‘image_url’)%>
<% end %>
<%= submit_tag %>
<%= end_form_tag %>
When the form is submitted to the controller, params[:product] will be
a hash of hashes, where each key is the id of a model object and the
corresponding value are the values from the form for that object. In
the controller, this could be used to update all product rows with
something like
Product.update(params[:product].keys, params[:product].values)
I would like to be able to do exactly the same thing, but on create.
Any idea how I could work this out?
I know (still from Agile Web Dev p. 212) that “you can pass create( )
an array of attribute hashes; it?ll create multiple rows in the
database and return an array of the corresponding model objects.”
But how would I go about setting up my view to pass this information
over?
I’ve looked all over the rails Wiki, googled for it, chatted on irc…
without much success…
Anyone has an idea about how to do that properly?
Thanks and best regards,
Tobie