1
我在Rails 5中構建了我的第一個API(曾經)作爲學習體驗。當試圖通過POST創建子記錄時,Rails 5 API - 父'必須存在'錯誤
我在--api模式下使用Rails並安裝了active_model_serializers gem。
該API基於船舶和航程之間的關係。
船has_many:航程和航程belongs_to:船。
使用郵差檢查航行API端點我得到這個作爲返回值:
{
"id": 1,
"tstd_id": 94583,
"year": 1722,
"began": null,
"trade_began": null,
"departed_africa": null,
"arrived_slaves": null,
"ended": null,
"length": null,
"middle_passage_length": null,
"port_departed": "Liverpool",
"ship": {
"id": 1,
"name": "Mary",
"flag": "British",
"rig": null,
"tonnage": null,
"standardized_tonnage": null,
"year_constructed": null,
"place_registered": null,
"year_registered": null,
"from": null,
"to": null
}
}
當我嘗試使用密鑰創建通過郵政的新航程:航程[船] [ID]價值:1我碰到的API返回「必須存在」
我從Rails的控制檯得到的錯誤是:
Started POST "/voyages" for 127.0.0.1 at 2016-07-28 11:03:47 +0100
Processing by VoyagesController#create as */*
Parameters: {"voyage"=>{"ship"=>{"id"=>"1"}, "year"=>"4"}}
Unpermitted parameter: ship
(0.2ms) begin transaction
(0.1ms) rollback transaction
[active_model_serializers] Rendered ActiveModel::Serializer::Null with ActiveModel::Errors (0.14ms)
Completed 422 Unprocessable Entity in 9ms (Views: 0.9ms | ActiveRecord: 0.4ms)
任何和所有幫助將不勝感激。
不幸的是,我通過POST傳遞ship_id而不是ship [id]時得到完全相同的錯誤。參數正在返回,因爲您建議他們應該但我在控制檯中收到一個不允許的參數ship_id。 –
這是因爲我在控制器允許的參數列表中沒有ship_id - 哎呀!謝謝! –