nathanvda / cocoon

Dynamic nested forms using jQuery made easy; works with formtastic, simple_form or default forms
http://github.com/nathanvda/cocoon
MIT License
3.08k stars 385 forks source link

link_to_add_association not adding correctly new items #576

Closed andreschav closed 4 years ago

andreschav commented 4 years ago

when clicked "link_to_add_association" adds the nested fields on the form, but all this added fields has the same id "persona_cuenta_bancarias_attributes_new_cuenta_bancarias_numero_cuenta" only the first item id is 0 "persona_cuenta_bancarias_attributes_0_numero_cuenta" and only the first item is saved or updated Controller:

class Persona < ApplicationRecord
  has_many :cuenta_bancarias, dependent: :destroy, inverse_of: :persona
  accepts_nested_attributes_for :cuenta_bancarias, allow_destroy: true
end

class CuentaBancaria < ApplicationRecord
  belongs_to :persona, optional:false
end

The View:

 <%= simple_form_for(@persona, :url => :crear_proveedor_personas, :method => :post, html: {novalidate: true, autocomplete: "off"}) do |f| %>
     <div class="form-row row">
       <div class="form-group col-md-3">
         <%= f.label :nombres, 'Nombre', class: 'control-label' %>
         <%= f.text_field :nombres, class: 'form-control', placeholder: 'Nombres' %>
       </div>
     </div>

     <div class="form-row row">
       <div class="form-group col-md-6">
         <%= f.label :email, 'E-mail personal', class: 'control-label' %>
         <%= f.text_field :email, class: 'form-control', placeholder: 'Correo electronico Personal' %>
       </div>
     </div>

     <!-- TABLA CONTENEDORA DE CUENTAS BANCARIAS -->
     <div class="panel panel-info proveedor_table">
       <div class="panel-heading" style="text-align: center; font-size: 18px; font-style: inherit">
         Cuentas Bancarias
       </div>
       <div class="col-md-12 panel-heading ">
         <div class="form-group">
           <div class="col-md-3 ">
             Num. Cuenta
           </div>
           <div class="col-md-2 ">
             Moneda
           </div>
           <div class="col-md-6 ">
             Entidad Financiera
           </div>
           <div class="col-md-1">
             <%= link_to_add_association '', f, :cuenta_bancarias, :class => "fa fa-plus fa-fw" %>
           </div>
         </div>
       </div>
       <div class="form-row row panel-body cuentas_bancarias">
         <%= f.simple_fields_for :cuenta_bancarias do |cb| %>
             <%= render 'cuenta_bancaria_fields', f: cb %>
         <% end %>
       </div>
     </div>
     <!-- FIN TABLA CONTENEDORA DE CUENTAS BANCARIAS -->

     <div class="row">
       <div class="form-group col-md-4">
         <%= f.submit :value => 'Guardar', :class => 'btn btn-primary' %>
       </div>
     </div>
 <% end %>

this is my bundle show

  • actioncable (5.2.4.2)
    • actionmailer (5.2.4.2)
    • actionpack (5.2.4.2)
    • actionview (5.2.4.2)
    • activejob (5.2.4.2)
    • activemodel (5.2.4.2)
    • activerecord (5.2.4.2)
    • activestorage (5.2.4.2)
    • activesupport (5.2.4.2)
    • addressable (2.5.2)
    • archive-zip (0.11.0)
    • arel (9.0.0)
    • autoprefixer-rails (9.4.3)
    • bcrypt (3.1.12)
    • bindex (0.5.0)
    • bootsnap (1.3.2)
    • bootstrap-sass (3.4.0)
    • builder (3.2.4)
    • bundler (1.17.3)
    • byebug (10.0.2)
    • cancancan (2.3.0)
    • capybara (3.12.0)
    • carrierwave (1.3.1)
    • carrierwave-i18n (0.2.0)
    • childprocess (0.9.0)
    • chromedriver-helper (2.1.0)
    • cocoon (1.2.14)
    • coderay (1.1.2)
    • coffee-rails (4.2.2)
    • coffee-script (2.4.1)
    • coffee-script-source (1.12.2)
    • concurrent-ruby (1.1.6)
    • crass (1.0.6)
    • daemons (1.3.1)
    • debase (0.2.2)
    • debase-ruby_core_source (0.10.4)
    • delayed_job (4.1.5)
    • delayed_job_active_record (4.1.3)
    • devise (4.5.0)
    • devise-i18n (1.7.0)
    • erubi (1.9.0)
    • execjs (2.7.0)
    • ffi (1.9.25)
    • font-awesome-rails (4.7.0.4)
    • globalid (0.4.2)
    • i18n (1.8.2)
    • io-like (0.3.0)
    • jbuilder (2.8.0)
    • jquery-datatables-rails (3.4.0)
    • jquery-rails (4.3.3)
    • jquery-ui-rails (6.0.1)
    • listen (3.1.5)
    • loofah (2.4.0)
    • mail (2.7.1)
    • marcel (0.3.3)
    • method_source (0.9.2)
    • mime-types (3.2.2)
    • mime-types-data (3.2018.0812)
    • mimemagic (0.3.4)
    • mini_mime (1.0.2)
    • mini_portile2 (2.4.0)
    • minitest (5.14.0)
    • msgpack (1.2.4)
    • multi_json (1.13.1)
    • nio4r (2.5.2)
    • nokogiri (1.10.9)
    • orm_adapter (0.5.0)
    • pdf-core (0.7.0)
    • pg (1.1.3)
    • prawn (2.2.2)
    • prawn-table (0.2.2)
    • pry (0.12.2)
    • pry-rails (0.3.9)
    • public_suffix (3.0.3)
    • puma (3.12.0)
    • rack (2.2.2)
    • rack-test (1.1.0)
    • rails (5.2.4.2)
    • rails-autocomplete (2.0.1)
    • rails-dom-testing (2.0.3)
    • rails-html-sanitizer (1.3.0)
    • railties (5.2.4.2)
    • rake (13.0.1)
    • ransack (2.1.1)
    • rb-fsevent (0.10.3)
    • rb-inotify (0.10.0)
    • regexp_parser (1.3.0)
    • responders (2.4.0)
    • rmagick (2.16.0)
    • ruby-debug-ide (0.6.1)
    • ruby_dep (1.5.0)
    • rubyzip (1.2.2)
    • sass (3.7.2)
    • sass-listen (4.0.0)
    • sass-rails (5.0.7)
    • sassc (2.0.0)
    • selenium-webdriver (3.141.0)
    • simple_form (5.0.2)
    • spring (2.0.2)
    • spring-watcher-listen (2.0.1)
    • sprockets (3.7.2)
    • sprockets-rails (3.2.1)
    • sqlite3 (1.3.13)
    • thor (1.0.1)
    • thread_safe (0.3.6)
    • tilt (2.0.9)
    • ttfunk (1.5.1)
    • turbolinks (5.2.0)
    • turbolinks-source (5.2.0)
    • tzinfo (1.2.6)
    • uglifier (4.1.20)
    • warden (1.2.8)
    • web-console (3.7.0)
    • websocket-driver (0.7.1)
    • websocket-extensions (0.1.4)
    • will_paginate (3.1.6)
    • xpath (3.2.0)

please help me to fix this.

nathanvda commented 4 years ago

This is weird, normally we replace the new_ceunta_bancaria with a random/unique number. If we do not replace it, at least the very first new item would still be savable (I think).

The only reason I seem to think of now, are a possible problems with inflections (since you are using a non-english language).

So I have two questions:

nathanvda commented 4 years ago

Closed due to inactivity.