我有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」, } ] } 謝謝大家 –