Search code examples
ruby-on-railsrubyactiverecordnested-attributesnested-form-for

Trouble about nested attributes form


This is my first question. I hope you can help me. I have two models.

    class Cliente < ActiveRecord::Base
      attr_accessible :cedula, :direccion, :nombres, :telefono
      validates :cedula, :direccion, :nombres, :telefono, :presence => true 
      validates :cedula, :uniqueness => { :message => "Cedula ya en uso" }
      has_many :facturas

    class Factura < ActiveRecord::Base
      attr_accessible :cliente_attributes, :iva, :numero, :subtotal, :total, :created_at
      belongs_to :cliente
      accepts_nested_attributes_for :cliente

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.

<h1>Nueva Factura</h1>

<%= form_for @factura do |f| %>
<% if @factura.errors.any? %>
    <h2>Errores:</h2>   
    <ul>
        <% @factura.errors.full_messages.each do |message| %>
            <li><%= message %></li>
        <% end %>
    </ul>
<% end %>
<p>
<div class = "contenedor_factura">
    <%= f.label :numero %><br />
    <%= f.number_field :numero %><br />
    <%= f.label :fecha %><br />
    <%= f.date_select :created_at %><br />
</div>
<div class = "contenedor_cliente">
    <%= f.fields_for @cliente do |builder| %>
        <%= builder.label :cedula, "Cédula" %>
        <%= builder.text_field :cedula %>
        <%= builder.label :nombres, "Nombres" %>
        <%= builder.text_field :nombres %>
        <%= builder.label :direccion, "Dirección" %><br />
        <%= builder.text_field :direccion %>
        <%= builder.label :telefono, "Teléfono" %><br />
        <%= builder.text_field :telefono %>
        <%= builder.hidden_field :id%>
    <% end %>
</div> 
<div class = "contenedor_productos">

</div>


<%= f.label :subtotal %><br />
<%= f.text_field :subtotal %>
<br />
<%= f.label :iva %><br />
<%= f.text_field :iva %>

 </p>
  <p>
    <%= f.submit "Agregar Nueva Factura" %>
  </p>
  <% end %>

When the Cliente is new i have no problem, it saves, but if Cliente exists i have this message

ActiveRecord::RecordNotFound in FacturasController#create

Couldn't find Cliente with ID=6 for Factura with ID=

What is my problem?

EDIT:

This is my FacturasController

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 => "Factura Guardada"
  else
    render "new"
  end
end

Solution

  • I'm not sure if nested_attrubes handles already created models. So we can just not depend on it.

    This should work

    def create
      cliente_attrs = params[:factura].delete :cliente
      @cliente = cliente_attrs[:id].present? ? Cliente.find(cliente_attrs[:id]) : User.create(user_attrs)
      @factura = cliente.facturas.build(params[:factura])
      if @factura.save
        redirect_to facturas_path, :notice => "Factura Guardada"
      else
        render "new"
      end
    end
    

    You can now delete the line accepts_nested_attributes_for :cliente