# Multiple in one form

**URL:** <https://www.ruby-forum.com/t/multiple-in-one-form/149111>\
**Category:** Rails\
**Created:** [September 20, 2008, 6:36pm UTC](https://www.ruby-forum.com/t/multiple-in-one-form/149111 "2008-09-20T18:36:59Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/49beb7/32.png) [@jney](https://www.ruby-forum.com/u/jney)\
**Post date:** [September 20, 2008, 6:36pm UTC](https://www.ruby-forum.com/t/multiple-in-one-form/149111/1 "2008-09-20T18:36:59Z")

</div>

hi,

I just try to deal with multiples model in one form,  
using ajax to add models. but got a weird params hash.

got:  
:master\_model =\> {  
:sub\_model =\> [  
{ :field\_1 =\> “1” },  
{ :field\_1 =\> “2”, :field\_2 =\> “3” },  
{ :field\_2 =\> “4” }  
]  
}

instead of :

:master\_model =\> {  
:sub\_model =\> [  
{ :field\_1 =\> “1” :field\_2 =\> “3” },  
{ :field\_1 =\> “2”, :field\_2 =\> “4” }  
]  
}

anybody can help me?

---

<div class="post-metadata">

**Author:** ![jney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/49beb7/32.png) [@jney](https://www.ruby-forum.com/u/jney)\
**Post date:** [September 21, 2008, 7:30pm UTC](https://www.ruby-forum.com/t/multiple-in-one-form/149111/2 "2008-09-21T19:30:18Z")

</div>

i just checked it again and it looks like a form\_remote\_for bug,  
because with a non ajax form\_for params are well formed,  
can someone confirm it?

---

<div class="post-metadata">

**Author:** ![jney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/49beb7/32.png) [@jney](https://www.ruby-forum.com/u/jney)\
**Post date:** [September 22, 2008, 1:29am UTC](https://www.ruby-forum.com/t/multiple-in-one-form/149111/3 "2008-09-22T01:29:51Z")

</div>

On Sep 22, 1:29 am, jney [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> i just checked it again and it looks like a form\_remote\_for bug,  
> because with a non ajax form\_for params are well formed,  
> can someone confirm it?

It’s impossible to confirm with the little information you’ve given.  
Care to elaborate?

---

<div class="post-metadata">

**Author:** ![jney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/49beb7/32.png) [@jney](https://www.ruby-forum.com/u/jney)\
**Post date:** [September 22, 2008, 12:48pm UTC](https://www.ruby-forum.com/t/multiple-in-one-form/149111/4 "2008-09-22T12:48:23Z")

</div>

Hi erol,

Try this :

\<% form\_for(@master\_model) do |f| %\>  
\<%= f.error\_messages %\>

\<%- fields\_for(‘master\_model[sub\_model][]’, SubModel.new) do |sm| -  
%\>  
\<%= sm.text\_field :field1 %\>  
\<%= sm.text\_field :field2 %\>  
\<%= sm.text\_field :field3 %\>  
\<%- end -%\>

\<%- fields\_for(‘master\_model[sub\_model][]’, SubModel.new) do |sm| -  
%\>  
\<%= sm.text\_field :field1 %\>  
\<%= sm.text\_field :field2 %\>  
\<%= sm.text\_field :field3 %\>  
\<%- end -%\>

\<%= f.submit "Create" %\>

\<% end %\>

then check params format (it should be ok), then replace “form\_for”  
with “remote\_form\_for” and check params format,  
“master\_model[sub\_model]” should be wrong

---

<div class="post-metadata">

**Author:** ![jney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/49beb7/32.png) [@jney](https://www.ruby-forum.com/u/jney)\
**Post date:** [September 25, 2008, 9:11pm UTC](https://www.ruby-forum.com/t/multiple-in-one-form/149111/5 "2008-09-25T21:11:58Z")

</div>

I am having this same problem, but I need to do this with ajax…  
any ideas? I’ve been racking my brain on this for a few hours now and  
am losing it,

---

<div class="post-metadata">

**Author:** ![jney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/49beb7/32.png) [@jney](https://www.ruby-forum.com/u/jney)\
**Post date:** [September 25, 2008, 10:42pm UTC](https://www.ruby-forum.com/t/multiple-in-one-form/149111/6 "2008-09-25T22:42:57Z")

</div>

On Thu, 2008-09-25 at 12:11 -0700, Nathan R wrote:

> > \<%= f.error\_messages %\>
> > 
> > \<%-fields\_for(‘master\_model[sub\_model][]’, SubModel.new) do |sm| -  
> > %\>  
> > \<%= sm.text\_field :field1 %\>  
> > \<%= sm.text\_field :field2 %\>  
> > \<%= sm.text\_field :field3 %\>  
> > \<%- end -%\>

I find way better results with  
form\_remote\_for([@master\_model, @sub\_model])

in cases where I need several instances of sub model  
I do something like this in the view

\<% unless @master\_model.sub\_models.nil? %\>  
\<%= render :partial =\> ‘sub\_model’, :collection =\>  
@master\_model.sub\_models, :object =\> @master\_model%\>  
\<% end %\>  
\<%= render :partial =\> ‘new\_sub\_model’, :object =\> [@master\_model,  
@sub\_model] %\>

then the partials use  
div\_for(sub\_model) so the controller can talk back ajax to each instance  
of the form

the new\_sub\_model partial sends to create,  
then create via rjs, refreshes the list of sub\_models and shows a new  
empty new\_sub\_model template
