Rails中的WhiteListing参数5

问题描述 投票:-2回答:3

如果从GUI传递数组/哈希,这与白名单参数有关。

以下是我想要的白名单。

  1. 但是在服务器端看到一个错误,说明未经许可的参数。

注意:Orderplaced字段的类型为JSONB


private
 def order_params
 #params.require(:order).permit(:ordertype, :orderplaced => [:itemname => [], :quantity => [], :unitprice => [], :tax => [], :discount => [], :itemtotalprice => [] ])
 params.require(:order).permit(:ordertype, :orderplaced => [ { itemname: [], quantity: [], unitprice: [], tax: [], discount: [], itemtotalprice: [] }])
#not working with either of the above statements.
 end
end

服务器端错误未允许的参数

Started POST "/orders" for 127.0.0.1 at 2018-01-03 20:00:23 +0530
Processing by OrdersController#create as HTML
  Parameters: {"order"=>{"ordertype"=>"Home Delivery", "totalprice"=>"30", "paymentmethod"=>"Cash", "orderplaced"=>{":itemname"=>{"0"=>"Potatoe"}, ":quantity"=>{"0"=>"1"}, ":unitprice"=>{"0"=>"10"}, ":tax"=>{"0"=>"0"}, ":discount"=>{"0"=>"0"}, ":itemtotalprice"=>{"0"=>"10"}}}, "utf8"=>"Γ£ô", "authenticity_token"=>"1etU+M03uuTl8wcGij1+qEaSFcp/UvgBu3g/xBh0Hmexm4rA1vtCc1mkIWFsw8XcfC2sz2e9TBSmSBZNA9KiNA==", "commit"=>"Create Order"}
Unpermitted parameters: ::itemname, ::quantity, ::unitprice, ::tax, ::discount, ::itemtotalprice
  Customer Load (0.0ms)  SELECT  "customers".* FROM "customers" ORDER BY "customers"."id" ASC LIMIT $1  [["LIMIT", 1]]
   (0.0ms)  BEGIN
  SQL (4.0ms)  INSERT INTO "orders" ("ordertype", "orderplaced", "totalprice", "paymentmethod", "created_at", "updated_at", "customer_id") VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING "id"  [["ordertype", "Home Delivery"], ["orderplaced", "{}"], ["totalprice", 30.0], ["paymentmethod", "Cash"], ["created_at", "2018-01-03 14:30:23.393041"], ["updated_at", "2018-01-03 14:30:23.393041"], ["customer_id", 1]]
   (8.0ms)  COMMIT

  1. 如果我使用params.require(:order).permit!,那么我也只看到第一个值的索引,即array [0]通过params传递并保存到DB。那我们怎么能得到休息呢。

GUI: 去AZ小说网POI

ruby-on-rails postgresql jsonb
3个回答
0
投票

非常感谢你的回复。

在进行上述更改后(前端代码尚未修复),我看到一个问题未定义方法Here is the UI i am using to pass the valuesorderplaced_pa​​rams`方法。


控制器代码:

each_with_object' for #<ActionController::Parameters:0xc17f808>
in

服务器端错误:

class OrdersController < ApplicationController

    def new
        @order=Order.new
    end

    def create
        @order=Order.new(fixed_order_params)
        @order.save
    end

    private
        def order_params
            params.require(:order).permit(:ordertype, :totalprice, :paymentmethod, {orderplaced: {":itemname": ["0"], ":quantity": ["0"], ":unitprice": ["0"], ":tax": ["0"], ":discount": ["0"], ":itemtotalprice": ["0"]}})
        end


        def orderplaced_params
            order_params[:orderplaced].each_with_object({}) do |(k,v), returning|
            returning[k.gsub(":","")] = v["0"]
             end
        end

        def fixed_order_params
            order_params.slice(:ordertype, :totalprice, :paymentmethod).merge!(orderplaced: orderplaced_params)
        end

    end

一行的前端代码:(通过增加索引值使用Javascript添加其余行)

Started POST "/orders" for 127.0.0.1 at 2018-01-04 19:21:44 +0530
Processing by OrdersController#create as HTML
  Parameters: {"order"=>{"ordertype"=>"Home Deivery", "totalprice"=>"25", "paymentmethod"=>"Online", "orderplaced"=>{":quantity"=>{"0"=>"2"}, ":unitprice"=>{"0"=>"10"}, ":tax"=>{"0"=>"0"}, ":discount"=>{"0"=>"0"}, ":itemtotalprice"=>{"0"=>"20"}}}, "utf8"=>"Γ£ô", "authenticity_token"=>"e2RyLPkDVmCdxNgSzmK/Ov4dTJmCS1ZiM6G3T4u+8TflUd2UCOwnDU1/usslNQiZSGJdI3hNym14wehV9ClHfA==", "itemname"=>"Battery", "commit"=>"Create Order"}
Completed 500 Internal Server Error in 12ms (ActiveRecord: 0.0ms)



NoMethodError (undefined method `each_with_object' for #<ActionController::Parameters:0xc17f808>):

app/controllers/orders_controller.rb:25:in `orderplaced_params'
app/controllers/orders_controller.rb:31:in `fixed_order_params'
app/controllers/orders_controller.rb:10:in `create'

而你的猜测是正确的!我希望<tr> <td><input id="order[orderplaced][:itemname][0]" name="order[orderplaced][:itemname][0]" type="text" /></td> <td><input id="order[orders_attributes][0][quantity]" name="order[orderplaced][:quantity][0]" type="text" /></td> <td><input id="order[orders_attributes][0][unitprice]" name="order[orderplaced][:unitprice][0]" type="text" /></td> <td><input id="order[orders_attributes][0][tax]" name="order[orderplaced][:tax][0]" type="text" /></td> <td><input id="order[orders_attributes][0][discount]" name="order[orderplaced][:discount][0]" type="text" /></td> <td><input id="order[orders_attributes][0][itemtotalprice]" name="order[orderplaced][:itemtotalprice][0]" type="text" /></td> </tr> 以这样的方式存储在DB中,我可以拉出所需的Key及其值来显示。

哪种方式存储数据更容易拉出来。

你出现的东西:

orderplaced

或者这样

Parameters: {
  "order"=>{
    "ordertype"=>"Home Delivery", 
    "totalprice"=>"30", 
    "paymentmethod"=>"Cash", 
    "orderplaced"=>[
      {
        ":itemname"=>{"0"=>"Potatoe"}, 
        ":quantity"=>{"0"=>"1"}, 
        ":unitprice"=>{"0"=>"10"}, 
        ":tax"=>{"0"=>"0"}, 
        ":discount"=>{"0"=>"0"}, 
        ":itemtotalprice"=>{"0"=>"10"}
      },
      {
        ":itemname"=>{"0"=>"Television"}, 
        ":quantity"=>{"0"=>"1"}, 
        ":unitprice"=>{"0"=>"10"}, 
        ":tax"=>{"0"=>"0"}, 
        ":discount"=>{"0"=>"0"}, 
        ":itemtotalprice"=>{"0"=>"10"}
      },
      {
        ":itemname"=>{"0"=>"Cable"}, 
        ":quantity"=>{"0"=>"1"}, 
        ":unitprice"=>{"0"=>"10"}, 
        ":tax"=>{"0"=>"0"}, 
        ":discount"=>{"0"=>"0"}, 
        ":itemtotalprice"=>{"0"=>"10"}
      }
    ]
  }, 
  "utf8"=>"Γ£ô", 
  "authenticity_token"=>"1etU+M03uuTl8wcGij1+qEaSFcp/UvgBu3g/xBh0Hmexm4rA1vtCc1mkIWFsw8XcfC2sz2e9TBSmSBZNA9KiNA==",
  "commit"=>"Create Order"
}


0
投票

您的代码存在许多问题。首先,您的白名单语法不正确。它应该更像是:

Parameters: {
  "order"=>{
    "ordertype"=>"Home Delivery", 
    "totalprice"=>"30", 
    "paymentmethod"=>"Cash", 
    "orderplaced"=>[
      {
        ":itemname"=>{"0"=>"Potatoe","1"=>"Television","2"=>"Cable"}, 
        ":quantity"=>{"0"=>"1","1"=>"1","2"=>"1"}, 
        ":unitprice"=>{"0"=>"10","1"=>"10","2"=>"10"}, 
        ":tax"=>{"0"=>"0","1"=>"0","2"=>"0"}, 
        ":discount"=>{"0"=>"0","1"=>"0","2"=>"0"}, 
        ":itemtotalprice"=>{"0"=>"10","1"=>"10","2"=>"10"}
      }
    ]
  }, 
  "utf8"=>"Γ£ô", 
  "authenticity_token"=>"1etU+M03uuTl8wcGij1+qEaSFcp/UvgBu3g/xBh0Hmexm4rA1vtCc1mkIWFsw8XcfC2sz2e9TBSmSBZNA9KiNA==",
  "commit"=>"Create Order"
}

在控制台中,这将为您提供:

params.require(:order).permit(:ordertype, :totalprice, :paymentmethod, {orderplaced: {":itemname": ["0"], ":quantity": ["0"], ":unitprice": ["0"], ":tax": ["0"], ":discount": ["0"], ":itemtotalprice": ["0"]}})

这有两个问题:

  1. 你的> params.require(:order).permit(:ordertype, :totalprice, :paymentmethod, {orderplaced: {":itemname": ["0"], ":quantity": ["0"], ":unitprice": ["0"], ":tax": ["0"], ":discount": ["0"], ":itemtotalprice": ["0"]}}) => {"ordertype"=>"Home Delivery", "totalprice"=>"30", "paymentmethod"=>"Cash", "orderplaced"=>{":itemname"=>{"0"=>"Potatoe"}, ":quantity"=>{"0"=>"1"}, ":unitprice"=>{"0"=>"10"}, ":tax"=>{"0"=>"0"}, ":discount"=>{"0"=>"0"}, ":itemtotalprice"=>{"0"=>"10"}}} 值仍然嵌入哈希所有密钥orderplaced(例如,"0"),以及
  2. 你的":itemname"=>{"0"=>"Potatoe"}键在开头都有一个orderplaced(例如,:

(因为你将":itemname"存储在JSONB列中并且这是有效的JSON,你可以忽略这些问题。但是,现在清理它将为你或你的同事带来痛苦的未来。)

您可以通过执行以下操作来解决此问题:

orderplaced

再次在控制台中,它会给你:

def orderplaced_params
  order_params[:orderplaced].each_with_object({}) do |(k,v), returning|
    returning[k.gsub(":","")] = v["0"]
  end
end

现在,您需要重新组合固定的order_params。就像是:

> orderplaced_params
 => {"itemname"=>"Potatoe", "quantity"=>"1", "unitprice"=>"10", "tax"=>"0", "discount"=>"0", "itemtotalprice"=>"10"}

哪个会给你:

def fixed_order_params
  order_params.slice(:ordertype, :totalprice, :paymentmethod).merge!(orderplaced: orderplaced_params)
end

我有一种感觉,这不是故事的结局。根据你的用户界面(你还在发布图片!BOO!),我本来期望> fixed_order_params => {"ordertype"=>"Home Delivery", "totalprice"=>"30", "paymentmethod"=>"Cash", "orderplaced"=>{"itemname"=>"Potatoe", "quantity"=>"1", "unitprice"=>"10", "tax"=>"0", "discount"=>"0", "itemtotalprice"=>"10"}} 是一个哈希数组。就像是:

orderplaced

(我假设您想捕获图片中显示的每一行,作为Parameters: { "order"=>{ "ordertype"=>"Home Delivery", "totalprice"=>"30", "paymentmethod"=>"Cash", "orderplaced"=>[ { ":itemname"=>{"0"=>"Potatoe"}, ":quantity"=>{"0"=>"1"}, ":unitprice"=>{"0"=>"10"}, ":tax"=>{"0"=>"0"}, ":discount"=>{"0"=>"0"}, ":itemtotalprice"=>{"0"=>"10"} }, { ":itemname"=>{"0"=>"Television"}, ":quantity"=>{"0"=>"1"}, ":unitprice"=>{"0"=>"10"}, ":tax"=>{"0"=>"0"}, ":discount"=>{"0"=>"0"}, ":itemtotalprice"=>{"0"=>"10"} }, { ":itemname"=>{"0"=>"Cable"}, ":quantity"=>{"0"=>"1"}, ":unitprice"=>{"0"=>"10"}, ":tax"=>{"0"=>"0"}, ":discount"=>{"0"=>"0"}, ":itemtotalprice"=>{"0"=>"10"} } ] }, "utf8"=>"Γ£ô", "authenticity_token"=>"1etU+M03uuTl8wcGij1+qEaSFcp/UvgBu3g/xBh0Hmexm4rA1vtCc1mkIWFsw8XcfC2sz2e9TBSmSBZNA9KiNA==", "commit"=>"Create Order" } JSONB列的一部分。)

在这种情况下,orderplaced_pa​​rams需要看起来更像:

orderplaced

这会给你:

def orderplaced_params
  order_params[:orderplaced].map do |order_line_item|
    order_line_item.each_with_object({}) do |(k,v), hsh|
      hsh[k.gsub(":","")] = v["0"]
    end
  end
end

有几点需要注意:

  • 它是“马铃薯”,而不是“Potatoe”。
  • 您应该学习fixed_order_params => {"ordertype"=>"Home Delivery", "totalprice"=>"30", "paymentmethod"=>"Cash", "orderplaced"=>[{"itemname"=>"Potatoe", "quantity"=>"1", "unitprice"=>"10", "tax"=>"0", "discount"=>"0", "itemtotalprice"=>"10"}, {"itemname"=>"Television", "quantity"=>"1", "unitprice"=>"10", "tax"=>"0", "discount"=>"0", "itemtotalprice"=>"10"}, {"itemname"=>"Cable", "quantity"=>"1", "unitprice"=>"10", "tax"=>"0", "discount"=>"0", "itemtotalprice"=>"10"}]} 来格式化代码。变量通常被强调,例如:ruby style guide而不是order_placed
  • 如果我对orderplaced的哈希数组是正确的,那么你需要修复你的前端来传递数组而不是哈希。
  • 你应该修复你的前端,这样你的order_placed键不会被order_placed(例如,:,而不是discount)前置。
  • 你应该修复你的前端,使你的:discount元素没有嵌入哈希(例如,order_placed,而不是'discount'=>'0'

最后两个将节省你所有':discount'=>{'0'=>'0'}order_placed_params业务,并将允许你回到使用fixed_order_params

跟进

避免:

order_params

尝试用NoMethodError (undefined method `each_with_object' for #<ActionController::Parameters:0xc17f808>): ActionController::Parameters转换为hash

to_h

0
投票

order_params[:orderplaced].to_h.each_with_object({}) do |(k,v), returning| returning[k.gsub(":","")] = v["0"] end []'为零:NilClass):`

作为初学者,我很难理解最新情况。

我知道这是一个数组问题。但不知道我哪里出错了。

NoMethodError (undefined method
© www.soinside.com 2019 - 2024. All rights reserved.