允许参数数组 [英] Permit array in params
本文介绍了允许参数数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下JSON:
{
"name": "pizza",
"ingredients": [
{"name": "tomato", "amount": 3, "unit": "un"},
{"name": "chesse", "amount": 100, "unit": "gr"}
]
}
然后我使用POST将这个JSON传递给我的控制器,那么我需要信任此参数,但是我无法允许哈希数组ingredients
.
And I pass this JSON using POST to my controller, then I need to trust this paramters but I'm not be able to permit the array of hash ingredients
.
如何允许它?
我尝试了params.permit(:ingredients).permit(:name, :amount, :unit).to_h
,但是没有用.
How to permit it?
I tried params.permit(:ingredients).permit(:name, :amount, :unit).to_h
, but it doesn't work.
推荐答案
params.permit(:name, :ingredients => [:name, :amount,:unit])
应该可以解决问题.
params.permit(:name, :ingredients => [:name, :amount,:unit])
should do the trick.
阅读嵌套参数.
这篇关于允许参数数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文