![](/img/trans.png)
[英]How to access nested json params in a post request with Ruby on Rails on the backend
[英]Ruby On Rails: how to extract nested parameters from POST request
我想從使用 POST 表單提交的params
哈希中獲取鍵id
和quantity
的值。
參數:
params
# => {...
"form"=>
{"name"=>"joe",
"date_sent"=>"09/28/2016",
"emp_num"=>"54552452",
"items_attributes"=>{"1474227471986"=>{"id"=>"3", "quantity"=>"5", "_destroy"=>"false"}, "1474227474062"=>{"id"=>"4", "quantity"=>"3", "_destroy"=>"false"}},
"comments"=>"af",
"accepted"=>"false"},
"commit"=>"Submit",
"m"=>"true"}
通過以下方式,您可以獲取所有嵌套項目屬性的id和數量。
params['form']['items_attributes'].values.collect { |value| { id: value['id'], quantity: value['quantity'] } }
輸出:
[ {:id => 3, :quantity => 5 }, { :id => 4, :quantity => 3 } ]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.