ruby - Rails 5 API - parent 'must exist' error when trying to create a child record via POST -


i'm building first api (ever) in rails 5 learning experience.

i'm using rails in --api mode , have active_model_serializers gem installed.

the api based on relationship between ships , voyages.

a ship has_many :voyages , voyage belongs_to :ship.

using postman check voyages api endpoint i'm getting return value:

{   "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   } } 

when try create new voyage via post using key: voyage[ship][id] value: 1 i'm getting return api of 'must exist'

the error i'm getting rails console is:

started post "/voyages" 127.0.0.1 @ 2016-07-28 11:03:47 +0100 processing voyagescontroller#create */* 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 activemodel::errors (0.14ms) completed 422 unprocessable entity in 9ms (views: 0.9ms | activerecord: 0.4ms) 

any , appreciated.

if have ship's id you, have send ship_id instead of ship[id]. parameters should like,

{ "voyage"=> { "ship_id" => "1", "year" => "4" } } 

Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -