Problem with 2.3 nested forms and new elements

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 :frowning:

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!