# Nested form is giving me the wrong params

**URL:** <https://www.ruby-forum.com/t/nested-form-is-giving-me-the-wrong-params/172721>\
**Category:** Rails\
**Created:** [July 26, 2009, 11:18pm UTC](https://www.ruby-forum.com/t/nested-form-is-giving-me-the-wrong-params/172721 "2009-07-26T23:18:14Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mwalsh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/91b2a8/32.png) [@mwalsh](https://www.ruby-forum.com/u/mwalsh)\
**Post date:** [July 26, 2009, 11:18pm UTC](https://www.ruby-forum.com/t/nested-form-is-giving-me-the-wrong-params/172721/1 "2009-07-26T23:18:14Z")

</div>

I am working on an invoice tracking system for my business and have  
gotten so close to getting this portion to work!

I have an Invoice model with many InvoiceItems.

I am following this example (which is based off of ryan daigle’s post)  
[http://transfs.com/devblog/2009/06/26/nested-forms-with-rails-2-3-helpers-and-javascript-tricks/](http://transfs.com/devblog/2009/06/26/nested-forms-with-rails-2-3-helpers-and-javascript-tricks/)  
and I’ve looked at a couple of other similar examples as well.

I can generate the fields just fine, but when I try to add more fields  
and save, I get the following message:

ActiveRecord::UnknownAttributeError in InvoicesController#update

unknown attribute: invoice\_items

Parameters:  
{“commit”=\>“Submit”,  
“invoice”=\>{“invoice\_items\_attributes”=\>{“1248641854778”=\>  
{“invoice\_items”=\>{“project\_id”=\>“9”,  
“\_delete”=\>“0”,  
“amount\_billed”=\>“48”}},  
“0”=\>{“project\_id”=\>“8”,  
“\_delete”=\>“0”,  
“id”=\>“29”,  
“amount\_billed”=\>“32.00”}},  
“date\_due(1i)”=\>“2009”,  
“date\_due(2i)”=\>“8”,  
“date\_due(3i)”=\>“9”},  
“client\_id”=\>“1”,  
“\_method”=\>“put”,  
“authenticity\_token”=\>“WiwqytxYy/KZSrO76kWKGCH2hMlgiCj1lSedQV3h6KA=”,  
“id”=\>“17”}

The second line of parameters looks like it goes one level too deep  
with the Hash key “invoice\_items”. If you go further down to where the  
project\_id is 8, that is the record that was already in existance.  
Below is the yaml output of the request.

Here is the same request converted to yaml:

— !map:HashWithIndifferentAccess  
commit: Submit  
invoice: !map:HashWithIndifferentAccess  
invoice\_items\_attributes: !map:HashWithIndifferentAccess  
“1248641854778”: !map:HashWithIndifferentAccess  
invoice\_items: !map:HashWithIndifferentAccess  
project\_id: “9”  
\_delete: “0”  
amount\_billed: “48”  
“0”: !map:HashWithIndifferentAccess  
project\_id: “8”  
\_delete: “0”  
id: “29”  
amount\_billed: “32.00”  
date\_due(1i): “2009”  
date\_due(2i): “8”  
date\_due(3i): “9”  
authenticity\_token: WiwqytxYy/KZSrO76kWKGCH2hMlgiCj1lSedQV3h6KA=  
\_method: put  
client\_id: “1”  
action: update  
id: “17”  
controller: invoices

My form basically looks like this:

\<% form\_for [@client, @invoice], setup\_invoice(@invoice) do |f| %\>  
\<%= f.error\_messages %\>

## Add Projects:

- 

```
  <% f.fields_for :invoice_items do |invoice_item| %>
    <%= invoice_item.collection_select :project_id,

```

@client.projects, :id, :title, { :include\_blank =\> true } %\>  
\<%= invoice\_item.label :amount\_billed, “Amount to bill” %\>  
\<%= invoice\_item.text\_field :amount\_billed %\>

```
    <% if invoice_item.object.new_record? %>
      <%= link_to_function "clear", "this.up('.invoice_item').remove

```

()" %\>  
\<% else %\>  
\<%= invoice\_item.check\_box ‘\_delete’ %\>  
\<%= invoice\_item.label ‘\_delete’, ‘Remove’ %\>  
\<% end %\>  
\<% end %\>

```
</li>

```

 \<%= link\_to\_new\_nested\_form "Add a Project", f, :invoice\_items %\> 
* * *

\<%= f.label :date\_due %\>  
 \<%= f.date\_select :date\_due, :default =\> 15.days.from\_now, :order =\> [:day, :month, :year], :start\_year =\> Time.now.year

```
                               %>

```

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

\<% end %\>

I’m just not sure how to change the code to make it work!

Here is what I have in my application\_helper.rb:

def generate\_html(form\_builder, method, options = {})  
options[:object] ||=  
form\_builder.object.class.reflect\_on\_association(method).klass.new  
options[:partial] ||= method.to\_s.singularize  
options[:form\_builder\_local] ||= :f  
form\_builder.fields\_for(method, options[:object], :child\_index =\>  
‘NEW\_RECORD’) do |f|  
render(:partial =\> options[:partial], :locals =\> { options  
[:form\_builder\_local] =\> f })  
end  
end

def link\_to\_new\_nested\_form(name, form\_builder, method, options =  
{})  
options[:object] ||=  
form\_builder.object.class.reflect\_on\_association(method).klass.new  
options[:partial] ||= method.to\_s.singularize  
options[:form\_builder\_local] ||= :f  
options[:element\_id] ||= method.to\_s  
options[:position] ||= :bottom  
link\_to\_function name do |page|  
html = generate\_html(form\_builder,  
method,  
:object =\> options[:object],  
:partial =\> options[:partial],  
:form\_builder\_local =\> options  
[:form\_builder\_local]  
)  
page \<\< %{  
$(‘#{options[:element\_id]}’).insert({ #{options  
[:position]}: “#{ escape\_javascript html }”.replace(/NEW\_RECORD/g, new  
Date().getTime()) });  
}  
end  
end

I’ve had other problems with this approach as well, but if I can at  
least get it to save to the db I should be okay.

---

<div class="post-metadata">

**Author:** ![mwalsh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/91b2a8/32.png) [@mwalsh](https://www.ruby-forum.com/u/mwalsh)\
**Post date:** [July 27, 2009, 12:10am UTC](https://www.ruby-forum.com/t/nested-form-is-giving-me-the-wrong-params/172721/2 "2009-07-27T00:10:55Z")

</div>

Three things I can offer advice on here. You seem to be like me in you  
post a lot of information…

So, first post all that code on [pastie.org](http://pastie.org) or gisthub so it’s ordered  
nicely and easy to view. Then, post the link back here.

Second, you ran a \<%= debug(params) %\> in your view to verify all  
parameters?

Third, your field does exist in the table you are querying?

If all those checks out, I’ll see if i can see anything on your new view  
code. Four eyes are better than 2. 🙂

---

<div class="post-metadata">

**Author:** ![mwalsh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/91b2a8/32.png) [@mwalsh](https://www.ruby-forum.com/u/mwalsh)\
**Post date:** [July 27, 2009, 2:11am UTC](https://www.ruby-forum.com/t/nested-form-is-giving-me-the-wrong-params/172721/3 "2009-07-27T02:11:29Z")

</div>

First of all, thanks for taking the time to recommend a better way of  
displaying my problem!

Here is the gist [http://gist.github.com/155971](http://gist.github.com/155971)

The problem arises when I use the helper method in my  
application\_helper.rb to create new fields for the nested form.

In my controller I build one instance of the invoice\_item  
(@invoice.invoice\_items.build) and that field works just fine, when  
viewing the parameters, it is the section that reads:

“0”: !map:HashWithIndifferentAccess  
project\_id: “8”  
\_delete: “0”  
id: “31”  
amount\_billed: “32.00”

It is the one above (in the params from the gist) that that looks  
similar (project\_id: 9, amount\_billed: “68”) that is the problem, it  
is getting nested too deep under “invoice\_items”…unnecessarily so it  
would seem. I have added my Invoice and InvoiceItems models to the  
gist as well.

Thanks for your help!

On Jul 26, 3:10 pm, Alpha B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![mwalsh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/91b2a8/32.png) [@mwalsh](https://www.ruby-forum.com/u/mwalsh)\
**Post date:** [July 27, 2009, 5:15am UTC](https://www.ruby-forum.com/t/nested-form-is-giving-me-the-wrong-params/172721/4 "2009-07-27T05:15:50Z")

</div>

I’m not saving anything before the new nested instance is created. I  
didn’t write the piece using :child\_index, but perhaps I should do  
some more research about that chunk of code. Thanks again for your  
help, I’ll try to post the solution when I find it!

On Jul 26, 6:17 pm, Alpha B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![mwalsh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/91b2a8/32.png) [@mwalsh](https://www.ruby-forum.com/u/mwalsh)\
**Post date:** [July 30, 2009, 4:51pm UTC](https://www.ruby-forum.com/t/nested-form-is-giving-me-the-wrong-params/172721/5 "2009-07-30T16:51:09Z")

</div>

Yet another stupid mistake. I wasn’t keeping track of my partials and  
I had nested my nested form, thus creating the extra level of params.  
Sheesh

---

<div class="post-metadata">

**Author:** ![mwalsh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/91b2a8/32.png) [@mwalsh](https://www.ruby-forum.com/u/mwalsh)\
**Post date:** [July 27, 2009, 3:17am UTC](https://www.ruby-forum.com/t/nested-form-is-giving-me-the-wrong-params/172721/6 "2009-07-27T03:17:36Z")

</div>

Hi Matt,

Well the code checks out fine from my end with regards to syntax.  
However, I have a question in regards to your invoice…

Are you saving anything at all before the new nested instance is  
created? The reason I ask is because you are using :child\_index and I  
was just curious whether you would run into issues with unused  
instances, or perhaps the issue is somewhere in that piece?

Other than that, I just don’t know mate…
