# Params error with: complex form+multiple models

**URL:** <https://www.ruby-forum.com/t/params-error-with-complex-form-multiple-models/155226>\
**Category:** Rails\
**Created:** [December 9, 2008, 5:05pm UTC](https://www.ruby-forum.com/t/params-error-with-complex-form-multiple-models/155226 "2008-12-09T17:05:01Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![davidstokar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4da419/32.png) [@davidstokar](https://www.ruby-forum.com/u/davidstokar)\
**Post date:** [December 9, 2008, 5:05pm UTC](https://www.ruby-forum.com/t/params-error-with-complex-form-multiple-models/155226/1 "2008-12-09T17:05:01Z")

</div>

Hello,

I’m trying to create a complex form with multiple models.

The problem is that the params hash is sent out of order to the  
controller, and the data is saved wrong in the database.

What I get is:  
Parameters: {“commit”=\>“Cadastrar”, “invoice”=\>{“mode”=\>“in”,  
“total”=\>“34”, “items\_attributes”=\>[{“item”=\>“3333333”},  
{“quantity”=\>“3333333”, “item”=\>“666666”}, {“price”=\>“3333333”,  
“quantity”=\>“666666”}, {“price”=\>“4666666”}], “company\_id”=\>“10”},…

But what I want is:  
Parameters: {“commit”=\>“Cadastrar”, “invoice”=\>{“mode”=\>“in”,  
“total”=\>“34”, “items\_attributes”=\>[{“item”=\>“3333333”,  
“quantity”=\>“3333333”, price"=\>“3333333”},{“item”=\>“666666”,  
“quantity”=\>“666666”,“price”=\>“666666”}], “company\_id”=\>“10”},…

Codes are:

Controller: [http://pastie.org/334963](http://pastie.org/334963)  
Models: [http://pastie.org/334965](http://pastie.org/334965)  
Views: [http://pastie.org/334967](http://pastie.org/334967)

Does anyone know my error?  
Any help will be thankfull.

David S.

---

<div class="post-metadata">

**Author:** ![davidstokar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4da419/32.png) [@davidstokar](https://www.ruby-forum.com/u/davidstokar)\
**Post date:** [December 10, 2008, 10:56pm UTC](https://www.ruby-forum.com/t/params-error-with-complex-form-multiple-models/155226/2 "2008-12-10T22:56:18Z")

</div>

up,

anyone?

---

<div class="post-metadata">

**Author:** ![davidstokar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4da419/32.png) [@davidstokar](https://www.ruby-forum.com/u/davidstokar)\
**Post date:** [December 13, 2008, 4:22pm UTC](https://www.ruby-forum.com/t/params-error-with-complex-form-multiple-models/155226/3 "2008-12-13T16:22:12Z")

</div>

Does any one know how to fix? Or any suggestion will be ok.  
I did not find the solution yet.

David S.

---

<div class="post-metadata">

**Author:** ![davidstokar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4da419/32.png) [@davidstokar](https://www.ruby-forum.com/u/davidstokar)\
**Post date:** [December 13, 2008, 4:53pm UTC](https://www.ruby-forum.com/t/params-error-with-complex-form-multiple-models/155226/4 "2008-12-13T16:53:32Z")

</div>

On Dec 13, 3:22 pm, David S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Does any one know how to fix? Or any suggestion will be ok.  
> I did not find the solution yet.

Given that it’s an ajax form (ie serialized by prototype), it’s worth  
checking that you form elements have unique dom ids as that sort of  
stuff will confuse prototype.

Fred

---

<div class="post-metadata">

**Author:** ![davidstokar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4da419/32.png) [@davidstokar](https://www.ruby-forum.com/u/davidstokar)\
**Post date:** [December 15, 2008, 2:14am UTC](https://www.ruby-forum.com/t/params-error-with-complex-form-multiple-models/155226/5 "2008-12-15T02:14:48Z")

</div>

Hi Fred,  
thanks for answered.

So, I have this code generated:

item quantity Price remover

item quantity Price remover

item quantity Price remover

item quantity Price remover

Or you can see the code here: [http://pastie.org/339225](http://pastie.org/339225)

The question is: Should this input() have a id different from the  
other ones? I’m using a partial (partial code: [http://pastie.org/334967](http://pastie.org/334967))  
to render this output. What should I do?

Sorry for all those question, but I’m a little noobe, and I have  
exhausted all other sources that I now.

Thanks,

David S.

---

<div class="post-metadata">

**Author:** ![davidstokar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4da419/32.png) [@davidstokar](https://www.ruby-forum.com/u/davidstokar)\
**Post date:** [December 15, 2008, 5:32pm UTC](https://www.ruby-forum.com/t/params-error-with-complex-form-multiple-models/155226/6 "2008-12-15T17:32:31Z")

</div>

On Sun, Dec 14, 2008 at 5:14 PM, David S.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> The question is: Should this input() have a id different from the  
> other ones? I

Absolutely, that’s basic HTML – an ID must be unique within a given  
page. And more so if you have JavaScript acting on things via ID.

## FWIW,

Hassan S. ------------------------ removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![davidstokar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4da419/32.png) [@davidstokar](https://www.ruby-forum.com/u/davidstokar)\
**Post date:** [December 15, 2008, 5:16pm UTC](https://www.ruby-forum.com/t/params-error-with-complex-form-multiple-models/155226/7 "2008-12-15T17:16:07Z")

</div>

So, now I know where is the error, but I still don’t know how to fix it.

When I use remote\_form\_for, the params hash gets messed up. If I use,  
form\_for, everything is sent ok. But, I want a Ajax for.

Does anyone know how to fix it?

thanks,

David S.

---

<div class="post-metadata">

**Author:** ![davidstokar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4da419/32.png) [@davidstokar](https://www.ruby-forum.com/u/davidstokar)\
**Post date:** [December 16, 2008, 12:35pm UTC](https://www.ruby-forum.com/t/params-error-with-complex-form-multiple-models/155226/8 "2008-12-16T12:35:30Z")

</div>

On Dec 16, 1:50 am, David S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Hi Hassan S.,  
> thanks for replay it.
> 
> So, to solve the problem I had to create a unique ID to each element.  
> That was easy I used this code:
> 
> \<% fields\_for “invoice[items\_attributes][#{Time.now.tv\_usec.to\_s}]”,  
> invoice\_item do |form\_invoice\_item| -%\>

If the forms you had right at the beginning only submitted incorectly  
because of the duplicate ids then I’d try going back to that original  
code and only changing  
\<%= form\_invoice\_item.text\_field :item %\>  
to  
\<%= form\_invoice\_item.text\_field :item, :id =\>  
“invoice\_items\_attributes\_item\_#{invoice\_item.id}” %\>

Fred

---

<div class="post-metadata">

**Author:** ![davidstokar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4da419/32.png) [@davidstokar](https://www.ruby-forum.com/u/davidstokar)\
**Post date:** [December 16, 2008, 2:50am UTC](https://www.ruby-forum.com/t/params-error-with-complex-form-multiple-models/155226/9 "2008-12-16T02:50:07Z")

</div>

Hi Hassan S.,  
thanks for replay it.

So, to solve the problem I had to create a unique ID to each element.  
That was easy I used this code:

\<% fields\_for “invoice[items\_attributes][#{Time.now.tv\_usec.to\_s}]”,  
invoice\_item do |form\_invoice\_item| -%\>

And with Time.now.tv\_usec.to\_s I created a unique ID for each group of  
elements, and e got the params hash like this:

Parameters: {“commit”=\>“Cadastrar”, “invoice”=\>{“mode”=\>“in”,  
“items\_attributes”=\>{ “239625”=\>[{“price”=\>“2”, “should\_destroy”=\>“”,  
“quantity”=\>“2”, “id”=\>“109”, “item”=\>“2”}],  
“48717”=\>[{“price”=\>“5”, “quantity”=\>“5”,  
“item”=\>“5”}],  
“238297”=\>[{“price”=\>“1”, “should\_destroy”=\>“”,  
“quantity”=\>“1”, “id”=\>“108”, “item”=\>“1”}],  
“240939”=\>[{“price”=\>“4”, “should\_destroy”=\>“”,  
“quantity”=\>“4”, “id”=\>“110”, “item”=\>“4”}],  
“236847”=\>[{“price”=\>“3”, “should\_destroy”=\>“”,  
“quantity”=\>“3”, “id”=\>“107”, “item”=\>“3”}]},  
“total”=\>“123”, “id”=\>“45”, “company\_id”=\>“10”},  
“authenticity\_token”=\>“560d84898abf599368ad8ef6972ffbb649ecf0ee”}  
( [http://pastie.org/340083](http://pastie.org/340083) ).

But I got a problem when accessing those attributes in my model.  
It was like this:  
…  
items\_attributes.each do |attributes|  
invoice\_item.build(attributes)  
…  
and I have to add a [1] to access the right attributes.  
…  
items\_attributes.each do |attributes|  
invoice\_item.build(attributes[1])  
…

The attributes[0] give me = 239625, and  
attributes[1] = price2should\_destroyquantity2id109item2

To create I new record, every thing was ok.  
To edit, I have to access the ID of the attributes variable… and I  
don’t know how. I have tried attributes[1][:id], attributes[1][0],  
attributes[1][id].  
Nothing seems to work. My model source code is here:  
[http://pastie.org/340088](http://pastie.org/340088)

So to sum up, how can I access the Id attributes of attributes[1]

Thanks

David S.
