【发布时间】:2015-03-16 09:01:45
【问题描述】:
我有一个带有 json api 的 rails 应用程序。到目前为止,我可以通过 POST 请求创建单个对象。
这很简单:
def create
customer = Customer.new(customer_params)
if customer.save
render json: customer, status: 201
else
render json: customer.errors, status: 422
end
end
和:
private
def customer_params
params.require(:customer).permit(:name, :city)
end
现在我想通过在我的 http 请求中传递一个数组来创建多个客户。像这样:
{
"customer": [
{
"name": "foo",
"city": "New York"
},
{
"name": "bar",
"city": "Chicago"
}
]
}
但是,我不知道如何处理这个问题。第一个问题是我的强参数函数不接受数组。 有没有办法使用强参数让我循环遍历数组?
【问题讨论】:
标签: ruby-on-rails activerecord strong-parameters