Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9097639
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T00:09:57+00:00 2026-06-17T00:09:57+00:00

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

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T00:09:58+00:00Added an answer on June 17, 2026 at 12:09 am

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this my first question on here. I hope someone can help. It is to
this is my first question in the forum, I hope you can help me
this is my first question on stack overflow, I hope you can help me.
This is my first question on Stack Overflow, so I hope that I'm clear
This is my first question in this forum, so i hope to be clear:
This is my first time here so I hope I post this question at
this is my first question I have searched for any launching activity questions and
I have another beginner's question that hopefully someone can help with. I'm trying to
I have a problem with programming jQuery and I hope that you can help
Since this is my first question here on stackoverflow I hope my question is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.