我有一个嵌套表格,用于具有嵌套图片属性的Item模型。 所有代码与https://gist.github.com/mhenrixon/978371基本相同 我一直在尝试使用jQuery文件上传,如railscast 381所示 但我只能上传一张图片。 据我所知,一切看起来都应该如此,我不知道问题是什么! 下面是尝试使用新项目添加多个图像后的日志
Started POST "/admin/items" for 127.0.0.1 at 2013-05-03 13:06:53 +0100
Processing by Admin::ItemsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"2xP9nuMsiWmGxrw2LLHNURahpdktDW7cf82WjOfXQh8=", "item"=>{"name"=>"ddd", "price"=>"", "description"=>"", "stock"=>"", "pictures_attributes"=>{"0"=>{"id"=>"", "attachable_id"=>"", "attachable_type"=>"Item", "image"=>#<ActionDispatch::Http::UploadedFile:0x007fea61735ae8 @headers="Content-Disposition: form-data; name=\"item[pictures_attributes][0][image]\"; filename=\"image.jpg\"\r\nContent-Type: image/jpeg\r\n", @tempfile=#<File:/var/folders/r3/1gzzfk2964b4tvvndtnr527m0000gn/T/RackMultipart20130503-25456-ynk0x0>, @content_type="image/jpeg", @original_filename="image.jpg">}}}, "button"=>""}
Admin Load (0.2ms) SELECT "admins".* FROM "admins" WHERE "admins"."id" = 1 LIMIT 1
(0.2ms) begin transaction
SQL (0.7ms) INSERT INTO "items" ("created_at", "description", "dimensions", "for_sale", "name", "price", "stock", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?, ?) [["created_at", Fri, 03 May 2013 12:06:55 UTC +00:00], ["description", ""], ["name", "ddd"], ["price", nil], ["stock", nil], ["updated_at", Fri, 03 May 2013 12:06:55 UTC +00:00]]
SQL (0.2ms) INSERT INTO "pictures" ("attachable_id", "attachable_type", "created_at", "image", "updated_at") VALUES (?, ?, ?, ?, ?) [["attachable_id", 47], ["attachable_type", "Item"], ["created_at", Fri, 03 May 2013 12:06:55 UTC +00:00], ["image", "image.jpg"], ["updated_at", Fri, 03 May 2013 12:06:55 UTC +00:00]]
(3.7ms) commit transaction
Redirected to http://localhost:3000/admin/items
答案 0 :(得分:0)
以数组表示法
使用文件输入的name属性<input type="file" name="myname" />
您的javascript可能如下所示:
if (formdata){
formdata.append("myname[]", files);
}
它也应该相反:
<input type="file" name="myname[]" />