Ruby on rails RecordNotFound接受\u嵌套的\u属性\u并属于\u
我明白了 ActiveRecord::RecordNotFound:对于ID为的订单,找不到ID为3的客户端= 尝试提交现有客户的订单时。通过表单或控制台键入:Ruby on rails RecordNotFound接受\u嵌套的\u属性\u并属于\u,ruby-on-rails,nested-forms,nested-attributes,Ruby On Rails,Nested Forms,Nested Attributes,我明白了 ActiveRecord::RecordNotFound:对于ID为的订单,找不到ID为3的客户端= 尝试提交现有客户的订单时。通过表单或控制台键入: Order.new(:client_attributes => { :id => 3 }) 支付表单.html.erb: <%= semantic_form_for @order, :url => checkout_purchase_url(:secure => true) do |f| %>
Order.new(:client_attributes => { :id => 3 })
支付表单.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 %>
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
checkout_-purchase_-url(:secure=>true)do | f |%>
订单.rb:
<%= 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 %>
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
类顺序:check\u client,则拒绝\u
def检查客户端(客户端属性)
如果_client=client.find(client_attr['id'])
self.client=\u client
返回真值
其他的
返回错误
结束
结束
结束
reject\u如果想法来自于我,但我记录了方法,甚至没有调用它!不管它叫什么名字 注:2020年2月
因为8年后我开始在这件事上投反对票,加上这张便条。虽然这是我8年前(手术后5年)采用的原始解决方案
我的原始修复
通过重载client_attributes=方法修复了此问题,如下所述:
如果您只需要现有客户机的新订单,而不需要修改客户机,则需要分配id
Order.new(client_id: 3)
这是在不重载client\u attributes=
方法和cleanest的情况下执行此操作的另一种方法
新订单现在拥有ID为3的客户端
如果还想更新ant客户端的属性,则必须添加客户端属性
,例如:
Order.new(client_id: 3, client_attributes: { id: 3, last_order_at: Time.current })
请参见2012年。也有相同的错误,即为现有模型创建一个新事物,与有许多关系,并且属于关系
通过向表单中添加现有模型(例如用户)id的隐藏字段来修复此问题
= form.input :user_id, as: :hidden
然后,创建了一个没有错误的新对象。如果您有很多关系,这将起作用。在Rails 6.0.2上测试
def clients_attributes =(attributes)
# Get IDs for any clients that already exist.
client_ids = attributes.values.map { |a| a[:id] }.compact
# Now find them all and move them to this section.
clients << Client.find(client_ids)
# Update them with standard `accepts_nested_attributes_for` behaviour.
super attributes
end
def客户端_属性=(属性)
#获取已存在的任何客户端的ID。
client_id=attributes.values.map{| a | a[:id]}.compact
#现在找到它们并将它们移动到该部分。
客户机即使通过id找到客户机,也不会创建另一个客户机吗?这不是Rails的方式。OP中的问题已经存在了十年,解决方案是使用客户端id
。虽然我理解这种方法可能有价值,但我从经验中了解到,坚持Rails的预期工作方式是有回报的,偏离会带来很多问题。因此,我强烈建议使用client\u id
,不要覆盖client\u attributes=
。这应该是公认的答案。解决OP的Rails方法是使用client\u id
。因此,要么1)在传递到后端之前在前端设置client_id
,要么2)从后端的client_属性向id分配client_id
。在这两种方法中的任何一种之后,接受\u嵌套的\u属性\u将起作用。好的,谢谢。我已将此更改为公认的答案。