Rails 5.2 与 fast_jsonapi 1.5、Ember 3.4
我在 Ember 中创建了一个这样的新标签记录:
let newTag = this.get('store').createRecord('tag', {
name: this.get('name')
});
newTag.save();
这会发送以下 json(如 Chrome 的 Network 选项卡中的 Request Payload 所示):
{"data":{"attributes":{"name":"photos","created_at":null,"updated_at":null},"type":"tags"}}
但是 Rails 只得到(通过在 TagsController 的 create 方法中打印出参数来验证):
{"controller"=>"tags", "action"=>"create"}
它会引发以下错误:
ActionController::ParameterMissing (param is missing or the value is empty: tag)
这是我的控制器代码:
# app/controllers/tags_controller.rb
class TagsController < ApplicationController
def create
tag = Tag.create!(tag_params)
render json: {status: "success"}
end
private
def tag_params
puts params
params.require(:tag).permit(:name)
end
end
让 ember 和 rails 相互理解的诀窍是什么?由于 ember 在有效负载中发送“类型”,我可以让 Rails 了解这是模型,从而满足我设置的“标签”出现在参数中的要求吗?