Hi
I have been following a thread here…
http://www.ruby-forum.com/topic/new?forum_id=3 but it did raise some new
curiosities that I hope someone can help address here
In particular it is with Duane’s super secret hack that works for
multiple child objects
There’s a super-secret and mostly undocumented way of doing this
that’s kind of handy, actually. It looks like this:<% for @comment in @post.comments %>
<%= text_field ‘comment[]’, ‘author’ %>
<%= text_field ‘comment[]’, ‘body’ %>
<% end %>The FormTagHelper will pick up the square brackets and realize you
want the index of the object in there, producing HTML like this:
The trouble is, while this would work for editing existing objects, is
there a similar workaround for instantiating multiple child objects?
Let’s say for instance I have a object Person which has_many
VehicleRegistrations, and I want to have a form that would enable me to
instantiate many VehicleRegistrations at once.
So supposing my form enables 2 vehicleregistrations to be entered
alongside with the fields for Person, do I use something like
<%= text_field ‘Person.VehicleRegistrations[]’, ‘licenseNumber’ %>
<%= text_field ‘Person.VehicleRegistrations[]’, ‘vehicleManufacturer’ %>
<%= text_field ‘Person.VehicleRegistrations[]’, ‘vehicleMake’ %>
<%= text_field ‘Person.VehicleRegistrations[]’, ‘licenseNumber’ %>
<%= text_field ‘Person.VehicleRegistrations[]’, ‘vehicleManufacturer’ %>
<%= text_field ‘Person.VehicleRegistrations[]’, ‘vehicleMake’ %>
And hope that formTagHelper will be smart to realise that Person has a
has_many relationship with VehicleRegistrations and munge in the field
values for the array of VehicleRegistration?
Thanks