[英]rails nested attributes rails record_not_found when update
I have 2 models like: 我有2种型号,例如:
class Customer < ActiveRecord::Base
has_many :passengers
accepts_nested_attributes_for :passengers, allow_destroy: true
end
class Passenger < ActiveRecord::Base
belongs_to :customer
end
customer_params
contain : customer_params
包含:
:name,...
:passengers_attributes: [:id, :name, :_destroy]
and when pass passengers_attributes
to update customer
(id=1) like 并且当通过
passengers_attributes
来更新customer
(id = 1)时,例如
{
"passengers_attributes": [
{
"name": "abc",
"id": 5
}
]
}
With passenger "abc"
is new record 与乘客
"abc"
是新记录
When i run customer.update_attributes!(customer_params)
, it raise error ActiveRecord::RecordNotFound: Couldn't find Passenger with ID=5 for Customer with ID=1
当我运行
customer.update_attributes!(customer_params)
,它引发错误ActiveRecord::RecordNotFound: Couldn't find Passenger with ID=5 for Customer with ID=1
Do you know this error? 你知道这个错误吗? i need your help.
我需要你的帮助。 Thanks
谢谢
Found your error here 在这里找到您的错误
{
"passengers_attributes": [
{
"name": "abc",
"id": 5
}
]
}
You can't send id in parameters here. 您不能在此处在参数中发送ID。 Id can't be modified or you can insert it manually.
无法修改ID,也可以手动将其插入。
Here your customer don't have passenger with id "5".that's why you are getting this error. 在这里,您的客户没有ID为“ 5”的乘客。这就是您收到此错误的原因。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.