# Same model multiple times, dynamically, in one form?

**URL:** <https://www.ruby-forum.com/t/same-model-multiple-times-dynamically-in-one-form/181013>\
**Category:** Rails\
**Created:** [January 3, 2010, 9:17pm UTC](https://www.ruby-forum.com/t/same-model-multiple-times-dynamically-in-one-form/181013 "2010-01-03T21:17:23Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![AlwaysCharging](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ee7513/32.png) [@AlwaysCharging](https://www.ruby-forum.com/u/AlwaysCharging)\
**Post date:** [January 3, 2010, 9:17pm UTC](https://www.ruby-forum.com/t/same-model-multiple-times-dynamically-in-one-form/181013/1 "2010-01-03T21:17:23Z")

</div>

I just can’t seem to figure this out. I can do this no problem using  
each\_with\_index and iterating through an array set up in the  
controller’s “New.” But that only allows me to finite number of  
records. I’d like for the user to be able to dynamically pick the  
number records just by clicking “Add Another.”

Ryan B. walks us through a similar problem in his Complex Forms  
series, specifically episode 2. However, that example is for 2 models  
in the one form (one model is nested). My form is just the 1 model  
but many times. This seems so simple, but I can’t figure it out. If  
I use his javascript helper method and pass in the variables to the  
partial, the input fields end up getting labeled the same as the  
fields I generated by iterating through my New array (i.e. all of the  
the fields are model[0][model\_attribute]). I don’t know how to  
increment the [0] using client side only javascript.

So any ideas?

Also, is this even the best way to solve this problem? I know there’s  
a slightly different method in the Advanced Rails Recipes book, again  
by Mr. Bates, but I can’t seem to adapt that to use the “add another”  
javascript.

This seems like it’d be a common problem, 1 model many times  
(dynamically) in 1 form, but haven’t been able to find enough out  
there to solve my problem. Any ideas? Am I even going about this the  
right way? Or, is there a better way?

Any help is greatly appreciated.

–

You received this message because you are subscribed to the Google  
Groups “Ruby on Rails: Talk” group.  
To post to this group, send email to removed\_email\_address@domain.invalid.  
To unsubscribe from this group, send email to  
removed\_email\_address@domain.invalid.  
For more options, visit this group at  
[http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).
