我正在尝试通过对Rails API的POST请求创建多个记录,但是我遇到了与params方面有关的问题。
正在发生的事情是未创建项目,因为模型中的回调正在查找不存在的值。我相信这与params有关。
这是我的代码:
{
"product": [
{
"name": "Strap On SoftRack Roof Rack by Otium Acupressure",
"link": "https://things.com/89902997836713867011111211111121/Strap-On-SoftRack-Roof-Rack-by-Otium",
"category_old": "",
"image_url": "https://thingd-media-ec1.com/default/899029978367138670_42120cf10765.jpg",
"price": "160",
"interest": "9999",
"company": "ACME",
"country": "USA",
"revenue": "999999"
},
{
"name": "Strap On SoftRack Roof Rack by Otium Acupressure 2",
"link": "https://things.com/Strap-On-SoftRack-Roof-Rack-by-Otium",
"category_old": "",
"image_url": "https://thingd-media-ec1.com/default/899029978367138670_42120cf10765.jpg",
"price": "160",
"interest": "9999",
"company": "ACME",
"country": "USA",
"revenue": "999999"
}
]
}
我的json调用的原始内容是:
Parameters: {"product"=>[{"name"=>"Strap On SoftRack Roof Rack by Otium Acupressure", "link"=>"https://fancy.com/things/89902997836713867011111211111121/Strap-On-SoftRack-Roof-Rack-by-Otium", "category_
old"=>"", "image_url"=>"https://thingd-media-ec1.thefancy.com/default/899029978367138670_42120cf10765.jpg", "price"=>"160", "interest"=>"9999", "company"=>"Fancy", "country"=>"USA", "revenue"=>"999999"},
{"name"=>"Strap On SoftRack Roof Rack by Otium Acupressure 2", "link"=>"https://fancy.com/things/Strap-On-SoftRack-Roof-Rack-by-Otium", "category_old"=>"", "image_url"=>"https://thingd-media-ec1.thefancy.
com/default/899029978367138670_42120cf10765.jpg", "price"=>"160", "interest"=>"9999", "company"=>"Fancy", "country"=>"USA", "revenue"=>"999999"}], "import"=>{}}
由此产生的参数如下:
首次通话时的总参数。
{"name"=>"Strap On SoftRack Roof Rack by Otium Acupressure", "link"=>"https://fancy.com/things/89902997836713867011111211111121/Strap-On-SoftRack-Roof-Rack-by-Otium", "category_old"=>"", "i
mage_url"=>"https://thingd-media-ec1.thefancy.com/default/899029978367138670_42120cf10765.jpg", "price"=>"160", "interest"=>"9999", "company"=>"Fancy", "country"=>"USA", "revenue"=>"999999"}
一旦它们处于循环中,它们就会出现。
https://graph.facebook.com/v2.5/<AD_ID>/adcreatives
答案 0 :(得分:2)
我相信你所寻找的更像是这样。在允许调用中放置根product
键告诉rails这将是一个包含所记录键的对象数组。
def batch_create
batch_create_product_params[:product].each do |product_params|
product = Product.new(product_params)
product.save
end
respond_to do |format|
format.json { render json: @product }
end
end
def batch_create_product_params
params.permit(product: [:name, :link, :category_old, :image_url, :price, :interest, :revenue, :end_date, :company, :country, :above_average_revenue])
end
答案 1 :(得分:0)
我不认为new
采用数组。我想你需要这样做 - 在强参数中使用p
param。
def batch_create
params[:product].each do |p|
product = Product.new(batch_create_product_params(p)) # Pass p
product.save
end
respond_to do |format|
format.json { render json: @product }
end
end
def batch_create_product_params(p) # Accept params
p.permit(:name, :link, :category_old, :image_url, :price, :interest, :revenue, :end_date, :company, :country, :above_average_revenue)
end
create
确实需要一个数组。也许这会对你的情况有所帮助。
def batch_create
product = Product.create(batch_create_product_params[:product])
respond_to do |format|
format.json { render json: @product }
end
end
def batch_create_product_params
params.permit(product: [:name, :link, :category_old, :image_url, :price, :interest, :revenue, :end_date, :company, :country, :above_average_revenue])
end