consigoRecordNotFound con accepts_nested_attributes_for y belongs_to
ActiveRecord :: RecordNotFound: No se pudo encontrar cliente con ID = 3 para una orden con ID =
cuando se trata de presentar un formulario de pedido para una Cliente existente. Esto sucede a través de la forma o la consola escribiendo:
Order.new(:client_attributes => { :id => 3 })
payment_form.html.erb:
<%= semantic_form_for @order, :url => checkout_purchase_url(:secure => true) do |f| %>
<%= f.inputs "Personal Information" do %>
<%= f.semantic_fields_for :client do |ff| %>
<%= ff.input :first_name %>
<%= ff.input :last_name %>
<!-- looks like semantic_fields_for auto-inserts a hidden field for client ID -->
<% end %>
<% end %>
<% end %>
Order.rb:
class Order < ActiveRecord::Base
belongs_to :client
accepts_nested_attributes_for :client, :reject_if => :check_client
def check_client(client_attr)
if _client = Client.find(client_attr['id'])
self.client = _client
return true
else
return false
end
end
end
La idea vino reject_if
desde here pero registré el método y ¡ni siquiera se llama! ¡No importa cuál es su nombre!
¿No termina creando otro cliente incluso si encuentra un cliente por identificación? – dubilla