我試圖將圖像附加到使用BackBone.js的模型,在Rails中按預期工作。DragonFly附件和BackBone.js
但與BackBone.js我不知道如何附加一個圖像,例如在展示模板中顯示它。
,你會在主旨看到我得到 None of the functions registered with #<Dragonfly::Analyser:0x000000028e2d60> were able to deal with the method call format(<Dragonfly::TempObject data="img2.jpg" >). You may need to register one that can.
這是我在BB.js模板中使用的形式:
<form id="new_album" enctype="multipart/form-data">
<input type="text" name="name" id="new_album_name">
<input type="file" name="cover_image" id="new_album_cover_image" >
<input type="submit" value="Add">
</form>
,這就是我得到:https://gist.github.com/4105311
這是GitHub上的完整回購:https://github.com/enricostano/FullstackAlbum
編輯
加入remotipart寶石和改變這樣的
<form id="new_album" method="post" enctype="multipart/form-data" data-remote="true" data-type="json">
<input type="text" name="name" id="new_album_name">
<input type="file" name="cover_image" id="new_album_cover_image" >
<input type="submit" value="Add">
</form>
我現在明白了形式這個Rails日誌
Started POST "/api/albums" for 127.0.0.1 at 2012-11-19 11:06:20 +0100
Processing by AlbumsController#create as JSON
Parameters: {"name"=>"caricati", "cover_image"=>"img2.jpg", "album"=>{"cover_image"=>"img2.jpg", "name"=>"caricati"}}
(0.1ms) begin transaction
None of the functions registered with #<Dragonfly::Analyser:0x0000000265c550> were able to deal with the method call format(<Dragonfly::TempObject data="img2.jpg" >). You may need to register one that can.
SQL (0.5ms) INSERT INTO "albums" ("cover_image_uid", "created_at", "name", "updated_at") VALUES (?, ?, ?, ?) [["cover_image_uid", "2012/11/19/11_06_20_434_file"], ["created_at", Mon, 19 Nov 2012 10:06:20 UTC +00:00], ["name", "caricati"], ["updated_at", Mon, 19 Nov 2012 10:06:20 UTC +00:00]]
(256.8ms) commit transaction
Completed 201 Created in 303ms (Views: 1.8ms | ActiveRecord: 257.4ms)
cache: [POST /] invalidate, pass
Started POST "/" for 127.0.0.1 at 2012-11-19 11:06:20 +0100
Processing by AlbumsController#index as JSON
Parameters: {"cover_image"=>#<ActionDispatch::Http::UploadedFile:0x007f95e878c1b8 @original_filename="img2.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"cover_image\"; filename=\"img2.jpg\"\r\nContent-Type: image/jpeg\r\n", @tempfile=#<File:/tmp/RackMultipart20121119-5822-hq8kbi>>, "name"=>"caricati", "remotipart_submitted"=>"true", "X-Requested-With"=>"IFrame", "X-Http-Accept"=>"application/json, text/javascript, */*; q=0.01"}
WARNING: Can't verify CSRF token authenticity
Album Load (0.2ms) SELECT "albums".* FROM "albums"
Completed 200 OK in 2ms (Views: 1.0ms | ActiveRecord: 0.2ms)
當我上傳文件的DragonFly在文件系統中創建2個文件: 12_37_59_129_file
和12_37_59_129_file.meta
第一個(應該是我上傳的文件)包含我剛剛上傳的文件的名稱。
嗨@andrew,我可以簡單地避免AJAX?我只需要直接發佈到Rails。 – enricostn
是的,但這仍然意味着您需要超越Backbone.sync'的限制才能這樣做。此外,結果將是服務器端的整頁重定向(您可能正在嘗試避免使用'Backbone')。 –
好吧,我會嘗試像[remotipart](https://github.com/JangoSteve/remotipart)寶石。任何提示? – enricostn