以下是我在控制器中提供创建方法的JSON
{
"sex":null,
"money":281,
"networth":281,
"property":[
{
"houses":"1",
"id":"5",
"father":"Jerry",
"mother":"Tanya",
"height":281,
"children":2,
"pets":24
},
{
"houses":"3",
"id":"5",
"father":"Rob",
"mother":"Anne",
"height":726,
"children":1,
"pets":55
}
]
}
我的控制器如下:
def create
person = Person.new
person.houses = person_params[:houses]
person.father = person_params[:father]
person.save
end
private
def person_params
params.permit(person: []).require(:person)
end
我无法找到如何从传递的JSON中提取数据,假设我想在JSON中保存数据库中的房屋实例或任何其他值,我将如何提取该数据?我想我的person_params有问题
答案 0 :(得分:0)
您需要格式化您的json格式,如下所示
{ "person": { "money": "281", "address": "123 Carrot Street" } }
您的create方法将收到此格式的参数
params[:person] as { "money" => "281", "address" => "123 Carrot Street" }