【发布时间】:2018-04-09 02:31:53
【问题描述】:
我正在 Rails 5 中构建我的第一个 API(有史以来)作为学习体验。
我在 --api 模式下使用 Rails 并安装了 active_model_serializers gem。
API 基于船舶和航次之间的关系。
一艘船 has_many :voyages 和一个航程 belongs_to :ship。
使用 Postman 检查 voyages 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
}
}
当我尝试通过 POST 使用 Key: voyage[ship][id] Value: 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)
我们将不胜感激。
【问题讨论】:
标签: ruby-on-rails ruby ruby-on-rails-5 active-model-serializers