How can I set wrapper data from a FormData ?
I use CarrierWave in Rails.
I'm calling a WebAPI from ajax.
This command has been successful.
curl -X POST \
https://site-url/api/books/ \
-H 'cache-control: no-cache' \
-H 'content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW' \
-F 'book[title]=test' \
-F book[image]=@kindle_3_0_library_ipad_iphone.jpg \
-F 'book[genre_id]=1'
This is the development.log at that time.
Parameters: {"book"=>{"title"=>"test", "image"=>#<ActionDispatch::Http::UploadedFile:0x0000000005f1d790 @tempfile=#<Tempfile:/tmp/RackMultipart20181016-37646-65mtjc.jpg>, @original_filename="kindle_3_0_library_ipad_iphone.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"book[image]\"; filename=\"kindle_3_0_library_ipad_iphone.jpg\"\r\nContent-Type: image/jpeg\r\n">, "genre_id"=>"1"}}
This code fails.
var request = new FormData();
request.append('title', title);
request.append('image', imageFile, imageFile.name);
request.append('genre_id', 1);
This is the development.log at that time.
Parameters: {"title"=>"test", "image"=>#<ActionDispatch::Http::UploadedFile:0x00007f01dc083240 @tempfile=#<Tempfile:/tmp/RackMultipart20181016-37646-3zmceu.jpg>, @original_filename="41fQlZLtDgL._SX337_BO1,204,203,200_.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"image\"; filename=\"41fQlZLtDgL._SX337_BO1,204,203,200_.jpg\"\r\nContent-Type: image/jpeg\r\n">, "genre_id"=>"1"}
Other tried this.
var request = new Object();
request.book = new FormData();
request.book.append('title', title);
request.book.append('image', imageFile, imageFile.name);
request.book.append('genre_id', 1);
This is the development.log at that time.
Parameters: {"object Object"=>nil}
I don't know how to set parameters that can be saved.
Thanks
This problem resolved.
request.append('book[title]', title);