我有2个模型,如:导轨嵌套属性轨道record_not_found当更新
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
包含:
:name,...
:passengers_attributes: [:id, :name, :_destroy]
并且当通passengers_attributes
更新customer
(ID = 1)等
{
"passengers_attributes": [
{
"name": "abc",
"id": 5
}
]
}
乘客是新的记录
当我运行customer.update_attributes!(customer_params)
,它引发错误ActiveRecord::RecordNotFound: Couldn't find Passenger with ID=5 for Customer with ID=1
你知道这个错误?我需要你的帮助。由于
什么是'客户'?该错误意味着您正在执行'Customer.find(5)'并且找不到该记录。你如何设法获取'客户'对象将回答你的问题,因为它似乎你正在试图找到一个不存在的记录。 – Nobita
我认为错误在于找不到ID为5的乘客。 (例如,customer.passengers.find(5)会引发RecordNotFound错误,您能否向我们展示表单视图? – jphager2
当我更改客户的乘客信息时,如上面提到的新Passeger记录,我们没有id ='5的乘客所以如果我想为这个客户创建一个新的乘客,我必须通过乘客参数不带ID,例如: { “passengers_attributes”:[ {name}:“abc”, } ] } 谢谢大家 –