强参数:如何处理嵌套的json代码?(Strong params: How to process nested json code?)

编程入门 行业动态 更新时间:2024-10-25 02:29:05
强参数:如何处理嵌套的json代码?(Strong params: How to process nested json code?)

我正在尝试编写一个处理JSON的更新方法。 JSON看起来像这样:

{ "organization": { "id": 1, "nodes": [ { "id": 1, "title": "Hello", "description": "My description." }, { "id": 101, "title": "fdhgh", "description": "My description." } ] } }

我的更新方法如下:

def update organization = Organization.find(params[:id]) nodes = params[:organization][:nodes] nodes.each do |node| n = Node.find(node[:id]) unless n.update_attributes(node_params) render json: organization, status: :failed end end render json: diagram, status: :ok end private def node_params params.require(:organization).permit(nodes: [:title, :description]) end

不幸的是, n.update_attributes(node_params)生成:

Unpermitted parameter: id Unpermitted parameter: id Unpermitted parameter: id (0.2ms) BEGIN (0.3ms) ROLLBACK *** ActiveRecord::UnknownAttributeError Exception: unknown attribute 'nodes' for Node.

有谁看到我做错了什么并写这个更新方法?

I'm trying to write an update method that processes JSON. The JSON looks like this:

{ "organization": { "id": 1, "nodes": [ { "id": 1, "title": "Hello", "description": "My description." }, { "id": 101, "title": "fdhgh", "description": "My description." } ] } }

My update method is as follows:

def update organization = Organization.find(params[:id]) nodes = params[:organization][:nodes] nodes.each do |node| n = Node.find(node[:id]) unless n.update_attributes(node_params) render json: organization, status: :failed end end render json: diagram, status: :ok end private def node_params params.require(:organization).permit(nodes: [:title, :description]) end

Unfortunately, n.update_attributes(node_params) generates:

Unpermitted parameter: id Unpermitted parameter: id Unpermitted parameter: id (0.2ms) BEGIN (0.3ms) ROLLBACK *** ActiveRecord::UnknownAttributeError Exception: unknown attribute 'nodes' for Node.

Does anyone see what I'm doing wrong and to write this update method?

最满意答案

在unless n.update_attributes(node_params)行之外,您尝试使用nodes_params更新Node n ,这些节点是JSON中的所有节点减去ID:

{"nodes"=>[{"title"=>"Hello", "description"=>"My description."}, {"title"=>"fdhgh", "description"=>"My description."}]}

您可以添加:id作为允许的节点参数,删除nodes分配步骤, node_params迭代node_params ,并在更新Node n时省略:id 。 例如,

def update organization = Organization.find(params[:id]) node_params.each do |node| n = Node.find(node[:id]) unless n.update_attributes(node.except(:id)) render json: organization, status: :failed end end render json: diagram, status: :ok end private def node_params params.require(:organization).permit(nodes: [:id, :title, :description]) end

On the unless n.update_attributes(node_params) line, you're trying to update Node n with nodes_params, which are all of the nodes from your JSON minus the ids:

{"nodes"=>[{"title"=>"Hello", "description"=>"My description."}, {"title"=>"fdhgh", "description"=>"My description."}]}

You could just add :id as a permitted node parameter, cut out the nodes assignment step, iterate over node_params instead, and just omit the :id when updating Node n. E.g.,

def update organization = Organization.find(params[:id]) node_params.each do |node| n = Node.find(node[:id]) unless n.update_attributes(node.except(:id)) render json: organization, status: :failed end end render json: diagram, status: :ok end private def node_params params.require(:organization).permit(nodes: [:id, :title, :description]) end

更多推荐

本文发布于:2023-08-07 20:34:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1465618.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:嵌套   如何处理   参数   代码   process

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!