# Problem with 2.3 nested forms and new elements

**URL:** <https://www.ruby-forum.com/t/problem-with-2-3-nested-forms-and-new-elements/171233>\
**Category:** Rails\
**Created:** [June 29, 2009, 10:37am UTC](https://www.ruby-forum.com/t/problem-with-2-3-nested-forms-and-new-elements/171233 "2009-06-29T10:37:28Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![kinunt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/7c8e57/32.png) [@kinunt](https://www.ruby-forum.com/u/kinunt)\
**Post date:** [June 29, 2009, 10:37am UTC](https://www.ruby-forum.com/t/problem-with-2-3-nested-forms-and-new-elements/171233/1 "2009-06-29T10:37:28Z")

</div>

Hi,

I’m using the new nested forms functionality and I having problems with  
new created elements.

I have an invoice with lines. I would like to be able to create a new  
invoice, in the same form insert new lines dinamically and saving the  
invoice and the lines when everything is validated, not before.

The problem is that when I add a line dinamically to the form, the new  
line created have the same id of other lines and when saving the invoce  
only one line with the same id is saved.

Any pointer or pattern to do this different will be really appreciated  
because I’m blocked with this right now ☹

This is the ids generated in HTML when I add three lines:

```
<tr class="linea">

```

| ArtÃ­culo | Unids. | Precio | Total |
| --- | --- | --- | --- |
| Seleccione un artÃ­culo Bombillas Cebador | | | remove |
| Seleccione un artÃ­culo Bombillas Cebador | | | remove |
| | | | remove |

\*\*\*\*\*\*\*\*\*\* facturas\_controller.rb fragment \*\*\*\*\*\*\*\*\*\*  
def new  
@factura = Factura.new  
@factura.codigo = “#{Factura.last.id+1}-#{Date.today.year}”  
@factura.lineas.build  
@clientes = Cliente.find(:all, :order =\> ‘nombre’)  
@articulos = Articulo.find(:all, :order =\> ‘nombre’)

```
respond_to do |format|
  format.html # new.html.erb
  format.xml { render :xml => @factura }
end

```

end

\*\*\*\*\*\*\*\*\*\* facturas\_helper.rb fragment \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
def add\_linea\_link(name, form)  
link\_to\_function name do |page|  
nueva\_linea = @factura.lineas.build  
linea = render(:partial =\> ‘linea’, :locals =\> { :pf =\> form,  
:linea =\> nueva\_linea})  
page \<\< %{  
var new\_linea\_id = “new\_” + new Date().getTime();  
$(‘lineas’).insert({ bottom: “#{ escape\_javascript  
linea }”.replace(/new\_\d+/g, new\_linea\_id) });  
}  
end  
end

\*\*\*\*\*\*\*\*\*\* \_form.erb.html from facturas fragment \*\*\*\*\*\*\*\*\*\*

```
<%= render :partial => 'linea', :collection => @factura.lineas,

```

:locals =\> { :pf =\> factura\_form } %\>

| ArtÃ­culo | Unids. | Precio | Total |
| --- | --- | --- | --- |

\<%= add\_linea\_link "AÃ±adir linea", factura\_form %\>

\*\*\*\*\*\*\*\*\*\* \_linea.erb.rhtml fragment \*\*\*\*\*\*\*\*\*\*\*\*

\<% pf.fields\_for :lineas, linea do |linea\_form| %\> \<%= linea\_form.collection\_select(:articulo\_id, @articulos, :id, :nombre, {:prompt =\> 'Seleccione un artÃ­culo'}) %\> \<%= linea\_form.text\_field :unidades, :onkeyup =\> "sayhola(this)" %\> \<%= linea\_form.text\_field :precio %\> ALGO \<%= link\_to\_function "remove", "$(this).up('.linea').remove()" %\> \<% end %\>

Thanks!
