# \_form rendering question for adding multiple child objects

**URL:** <https://www.ruby-forum.com/t/form-rendering-question-for-adding-multiple-child-objects/100841>\
**Category:** Rails\
**Created:** [February 4, 2006, 12:09am UTC](https://www.ruby-forum.com/t/form-rendering-question-for-adding-multiple-child-objects/100841 "2006-02-04T00:09:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![generik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/4da419/32.png) [@generik](https://www.ruby-forum.com/u/generik)\
**Post date:** [February 4, 2006, 12:09am UTC](https://www.ruby-forum.com/t/form-rendering-question-for-adding-multiple-child-objects/100841/1 "2006-02-04T00:09:24Z")

</div>

Hi

I have been following a thread here…  
[http://www.ruby-forum.com/topic/new?forum\_id=3](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

---

<div class="post-metadata">

**Author:** ![generik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/4da419/32.png) [@generik](https://www.ruby-forum.com/u/generik)\
**Post date:** [February 4, 2006, 12:25am UTC](https://www.ruby-forum.com/t/form-rendering-question-for-adding-multiple-child-objects/100841/2 "2006-02-04T00:25:39Z")

</div>

> 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:

It’s not really a super secret hack; it is mentioned in AWDWR. And it’s  
mentioned in passing in the API docs here, just before the Methods  
listing:  
[http://rails.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html](http://rails.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html)

---

<div class="post-metadata">

**Author:** ![generik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/4da419/32.png) [@generik](https://www.ruby-forum.com/u/generik)\
**Post date:** [February 4, 2006, 4:14am UTC](https://www.ruby-forum.com/t/form-rendering-question-for-adding-multiple-child-objects/100841/3 "2006-02-04T04:14:46Z")

</div>

I’m trying to do the same kind of thing, and in fact, I’ve also got a  
thread started about this issue (although no one has replied yet).

As far as I can tell, doing it the way you suggest doesn’t work because  
Rails doesn’t know the id’s yet for the new objects, and so can’t setup  
the arrays in the template. I’ve tried assigned the objects to an array  
in the controller, and then explicitly reference that array in the  
template, but is says that is not an allowed name for an instance  
variable.

I’ll be watching with interest for any suggestions on this problem.
