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:

<table id="lineas">
    <tbody><tr>
      <th>Artículo</th>
      <th>Unids.</th>
      <th>Precio</th>
      <th>Total</th>
    </tr>

    <tr class="linea">

  <td><select id="factura_lineas_attributes_0_articulo_id"
name="factura[lineas_attributes][0][articulo_id]"><option
value="">Seleccione un artículo</option>
<option value="1">Bombillas</option>
<option value="2">Cebador</option></select></td>
  <td><input id="factura_lineas_attributes_0_unidades"
name="factura[lineas_attributes][0][unidades]" onkeyup="sayhola(this)"
size="30" type="text"></td>
  <td><input id="factura_lineas_attributes_0_precio"
name="factura[lineas_attributes][0][precio]" size="30" type="text"></td>

  <td><a href="#" onclick="$(this).up('.linea').remove(); return
false;">remove</a></td>

</tr>

   </tbody><tbody><tr class="linea">

  <td><select id="factura_lineas_attributes_1_articulo_id"
name="factura[lineas_attributes][1][articulo_id]"><option
value="">Seleccione un artículo</option>
<option value="1">Bombillas</option>
<option value="2">Cebador</option></select></td>
  <td><input id="factura_lineas_attributes_1_unidades"
name="factura[lineas_attributes][1][unidades]" onkeyup="sayhola(this)"
size="30" type="text"></td>

  <td><input id="factura_lineas_attributes_1_precio"
name="factura[lineas_attributes][1][precio]" size="30" type="text"></td>

  <td><a href="#" onclick="$(this).up('.linea').remove(); return
false;">remove</a></td>

</tr>
</tbody><tbody><tr class="linea">

  <td><select id="factura_lineas_attributes_1_articulo_id"
name="factura[lineas_attributes][1][articulo_id]"></td>

  <td><input id="factura_lineas_attributes_1_unidades"
name="factura[lineas_attributes][1][unidades]" size="30"
type="text"></td>
  <td><input id="factura_lineas_attributes_1_precio"
name="factura[lineas_attributes][1][precio]" size="30" type="text"></td>

  <td><a href="#" onclick="$(this).up('.linea').remove(); return
false;">remove</a></td>

</tr>
</tbody></table>

********** 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 **********
  <table id="lineas">
    <tr>
      <th>Artículo</th>
      <th>Unids.</th>
      <th>Precio</th>
      <th>Total</th>
    </tr>

    <%= render :partial => 'linea', :collection => @factura.lineas,
:locals => { :pf => factura_form } %>
   </table>

  <p>
    <%= add_linea_link "Añadir linea", factura_form %>
  </p>

********** _linea.erb.rhtml fragment ************
<tr class="linea">
<% pf.fields_for :lineas, linea do |linea_form| %>
  <td><%= linea_form.collection_select(:articulo_id, @articulos, :id,
:nombre, {:prompt => 'Seleccione un artículo'}) %></td>
  <td><%= linea_form.text_field :unidades, :onkeyup => "sayhola(this)"
%></td>
  <td><%= linea_form.text_field :precio %></td>
  <td id="total">ALGO</td>
  <td><%= link_to_function "remove", "$(this).up('.linea').remove()"
%></td>
<% end %>
</tr>

Thanks!
-- 
Posted via http://www.ruby-forum.com/.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to