首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用Rails强参数反序列化Json-Api

使用Rails强参数反序列化Json-Api
EN

Stack Overflow用户
提问于 2015-09-10 08:58:27
回答 4查看 6.8K关注 0票数 23

我正在使用带有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(...),,则服务器会将其与所有内容一起保存为空(我目前没有对注释模型设置任何约束):

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

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32491360

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档