2013-05-03 55 views
0

我有一個嵌套的窗體,用於具有圖片嵌套屬性的項目模型。 所有的代碼基本上是相同的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

使用您的文件輸入數組中的符號名稱屬性

<input type="file" name="myname" /> 

您的JavaScript可以像添加多張圖片後:

if (formdata){ 
    formdata.append("myname[]", files); 
} 

它也應該反過來工作:

<input type="file" name="myname[]" /> 
+0

我已經更改name =「item [pictures_attributes] [0] [image]」的名稱屬性,如果它以數組結尾(它被設置爲name =「item [pictures_attributes ] [0] [圖像] []「) – 2013-05-03 12:22:47