【发布时间】:2015-09-10 00:58:26
【问题描述】:
我正在使用带有 EmberCLI 和 Rails 的 Active Model Serializers 0.10.x,同时尝试将 Json-Api 作为适配器。 GET 请求正在工作,但即使我尝试实现 jimbeaudoin here 描述的 rails strong_parameters 解决方案,活动模型的反序列化也没有。
我最近一次保存评论的尝试:
有效载荷:
{"data":{
"attributes": {"soft_delete":false,"soft_delete_date":null,"text":"hjfgfhjghjg","hidden":false,"empathy_level":0},
"relationships":{
"user":{"data":{"type":"users","id":"1"}},
"post":{"data":{"type":"posts","id":"1"}}},"type":"comments"}}
控制台输出:
Completed 400 Bad Request in 13ms (ActiveRecord: 8.6ms)
ActionController::ParameterMissing (param is missing or the value is empty: data):
评论控制器:
class Api::V1::CommentsController < MasterApiController
respond_to :json
...
def create
render json: Comment.create(comment_params)
end
...
def comment_params
#Deserialization issues... Waiting for #950 https://github.com/rails-api/active_model_serializers/pull/950
params.require(:data).require(:attributes).permit(:text, :user_id, :post_id, :empathy_level, :soft_delete_date, :soft_delete, :hidden)
end
end
请注意,如果我将参数设置为仅 params.permit(...),服务器会将其保存为空值(我暂时没有对 cmets 模型设置任何约束):
data: {id: "9", type: "comments",…}
attributes: {soft_delete: null, soft_delete_date: null, text: null, hidden: null, empathy_level: null}
id: "9"
relationships: {post: {data: null}, user: {data: null}}
type: "comments"
您可以访问完整代码here。
【问题讨论】:
-
你解决了这个问题吗?