Note that there are some explanatory texts on larger screens.

plurals
  1. POTrouble about nested attributes form
    primarykey
    data
    text
    <p>This is my first question. I hope you can help me. I have two models.</p> <pre><code> class Cliente &lt; ActiveRecord::Base attr_accessible :cedula, :direccion, :nombres, :telefono validates :cedula, :direccion, :nombres, :telefono, :presence =&gt; true validates :cedula, :uniqueness =&gt; { :message =&gt; "Cedula ya en uso" } has_many :facturas class Factura &lt; ActiveRecord::Base attr_accessible :cliente_attributes, :iva, :numero, :subtotal, :total, :created_at belongs_to :cliente accepts_nested_attributes_for :cliente </code></pre> <p>I want in the facturas#new view can create or edit Cliente. If exists update or if not exists create. I am using nested attributes. If exists I uses javascript to fill text fields. If not exists I fill text field and save when Factura save. This is facturas#new view.</p> <pre><code>&lt;h1&gt;Nueva Factura&lt;/h1&gt; &lt;%= form_for @factura do |f| %&gt; &lt;% if @factura.errors.any? %&gt; &lt;h2&gt;Errores:&lt;/h2&gt; &lt;ul&gt; &lt;% @factura.errors.full_messages.each do |message| %&gt; &lt;li&gt;&lt;%= message %&gt;&lt;/li&gt; &lt;% end %&gt; &lt;/ul&gt; &lt;% end %&gt; &lt;p&gt; &lt;div class = "contenedor_factura"&gt; &lt;%= f.label :numero %&gt;&lt;br /&gt; &lt;%= f.number_field :numero %&gt;&lt;br /&gt; &lt;%= f.label :fecha %&gt;&lt;br /&gt; &lt;%= f.date_select :created_at %&gt;&lt;br /&gt; &lt;/div&gt; &lt;div class = "contenedor_cliente"&gt; &lt;%= f.fields_for @cliente do |builder| %&gt; &lt;%= builder.label :cedula, "Cédula" %&gt; &lt;%= builder.text_field :cedula %&gt; &lt;%= builder.label :nombres, "Nombres" %&gt; &lt;%= builder.text_field :nombres %&gt; &lt;%= builder.label :direccion, "Dirección" %&gt;&lt;br /&gt; &lt;%= builder.text_field :direccion %&gt; &lt;%= builder.label :telefono, "Teléfono" %&gt;&lt;br /&gt; &lt;%= builder.text_field :telefono %&gt; &lt;%= builder.hidden_field :id%&gt; &lt;% end %&gt; &lt;/div&gt; &lt;div class = "contenedor_productos"&gt; &lt;/div&gt; &lt;%= f.label :subtotal %&gt;&lt;br /&gt; &lt;%= f.text_field :subtotal %&gt; &lt;br /&gt; &lt;%= f.label :iva %&gt;&lt;br /&gt; &lt;%= f.text_field :iva %&gt; &lt;/p&gt; &lt;p&gt; &lt;%= f.submit "Agregar Nueva Factura" %&gt; &lt;/p&gt; &lt;% end %&gt; </code></pre> <p>When the Cliente is new i have no problem, it saves, but if Cliente exists i have this message</p> <pre><code>ActiveRecord::RecordNotFound in FacturasController#create Couldn't find Cliente with ID=6 for Factura with ID= </code></pre> <p>What is my problem?</p> <p>EDIT: </p> <p>This is my FacturasController</p> <pre><code>def new @factura = Factura.new @cliente = @factura.build_cliente end def create @factura = Factura.new(params[:factura]) if @factura.save redirect_to facturas_path, :notice =&gt; "Factura Guardada" else render "new" end end </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload