Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Permit array in params

I have the following JSON:

{
    "name": "pizza",
    "ingredients": [
        {"name": "tomato", "amount": 3, "unit": "un"},
        {"name": "chesse", "amount": 100, "unit": "gr"}
    ]
}

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.

How to permit it? I tried params.permit(:ingredients).permit(:name, :amount, :unit).to_h, but it doesn't work.

like image 431
macabeus Avatar asked Dec 21 '25 01:12

macabeus


1 Answers

params.permit(:name, :ingredients => [:name, :amount,:unit]) should do the trick.

Read Nested Parameters.

like image 143
kiddorails Avatar answered Dec 23 '25 21:12

kiddorails