2016-10-13 73 views
0

我有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

你知道这个错误?我需要你的帮助。由于

+0

什么是'客户'?该错误意味着您正在执行'Customer.find(5)'并且找不到该记录。你如何设法获取'客户'对象将回答你的问题,因为它似乎你正在试图找到一个不存在的记录。 – Nobita

+0

我认为错误在于找不到ID为5的乘客。 (例如,customer.passengers.find(5)会引发RecordNotFound错误,您能否向我们展示表单视图? – jphager2

+0

当我更改客户的乘客信息时,如上面提到的新Passeger记录,我们没有id ='5的乘客所以如果我想为这个客户创建一个新的乘客,我必须通过乘客参数不带ID,例如: { “passengers_attributes”:[ {name}:“abc”, } ] } 谢谢大家 –

回答

0

在这里找到

{ 
    "passengers_attributes": [ 
    { 
     "name": "abc", 
     "id": 5 
    } 
    ] 
} 

你的错误你不能在参数发送ID在这里。 ID无法修改,或者您可以手动插入。

在这里,您的客户没有乘客身份证“5”。这就是为什么你得到这个错误。

+0

passengers_attributes是nested_attributes,所以id需要更新。 – jphager2

+0

你有没有记录ID为5? – user100693