【发布时间】:2019-12-04 16:38:58
【问题描述】:
如果模型包含一些关系,如何在使用 JSON API 时使用 Pundit strong parameters? 我已经posted 提出了一个问题,该问题解释了在单个模型的情况下如何使用它。 儿子,这是可行的:
# posts_controller.rb
def update
if @post.update(permitted_attributes(@post))
render jsonapi: @post
else
render jsonapi: @post.errors, status: :unprocessable_entity
end
end
private
def set_post
@post = Post.find(params[:id])
end
def post_params
ActiveModelSerializers::Deserialization.jsonapi_parse(
params,
only: [:title, :body, :user]
)
end
def pundit_params_for(_record)
params.fetch(:data, {}).fetch(:attributes, {})
end
不幸的是,它将无法提取请求 JSON 的 relationships 块中定义的模型,例如:
"relationships"=>{"country"=>{"data"=>{"type"=>"countries", "id"=>"1"}}, "language"=>{"data"=>{"type"=>"languages", "id"=>"245"}}}
有什么想法吗?
【问题讨论】: