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:
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 **********
ArtÃculo | Unids. | Precio | Total |
---|
<%= add_linea_link "Añadir linea", factura_form %>
********** _linea.erb.rhtml fragment ************
Thanks!