2012-10-31 62 views
2

我正在通過創建一個簡單的圖像板來學習Rails。我希望用戶能夠將圖像上傳到服務器,然後我將能夠爲他們提供服務。使用Rails,Paperclip和Backbone上傳文件

我正在使用rails-backbone和paperclip。

下面是相關的部分:

應用程序/模型/ image.rb

class Image < ActiveRecord::Base 
    attr_accessible :url 
    attr_accessible :data 
    has_attached_file :data, :styles => { :medium => "300x300>", :thumb => "100x100>" } 
end 

應用程序/資產/ Java腳本/骨幹網/模板/圖像/ submit.jst.ejs

<form id="new-image" name="image" data-remote="true" enctype="multipart/form-data"> 
    <div class="field"> 
    <label for="data"> image:</label> 
    <input type="file" name="data" id="data"> 
    </div> 

    <div class="actions"> 
    <input type="submit" value="Create Image" /> 
    </div> 
</form> 

app/controllers/images_controller.rb

def create 
    @image = Image.new(params[:image]) 
    respond_to do |format| 
    if @image.save 
     format.html { redirect_to @image, notice: 'Image was successfully created.' } 
     format.json { render json: @image, status: :created, location: @image } 
    else 
     format.html { render action: "new" } 
     format.json { render json: @image.errors, status: :unprocessable_entity } 
    end 
    end 
end 

我也跑這種遷移:

class AddAttachmentDataToImages < ActiveRecord::Migration 
    def self.up 
    add_attachment :images, :data 
    end 

    def self.down 
    remove_attachment :images, :data 
    end 
end 

在試圖保存一個名爲「fruits.png」,我得到這個輸出在控制檯文件:

Started POST "/images" for 127.0.0.1 at 2012-10-31 00:55:07 -0700 
Processing by ImagesController#create as JSON 
    Parameters: {"image"=>{"url"=>nil, "data"=>"C:\\fakepath\\fruits.png"}} 
Completed 500 Internal Server Error in 2ms 

Paperclip::AdapterRegistry::NoHandlerError (No handler found for "C:\\fakepath\\fruits.png"): 
    app/controllers/images_controller.rb:16:in `new' 
    app/controllers/images_controller.rb:16:in `create' 

任何幫助,將不勝感激!謝謝!

+0

可能的重複:http://stackoverflow.com/questions/10033425/paperclip-exception-paperclipadapterregistrynohandlererror – prusswan

+0

似乎是相同的錯誤,除非它看起來像解決方案是告訴表單窗體是多部分。我的表單的enctype =「multipart/form-data」,所以我不認爲這是問題。 –

+0

另一個:http://stackoverflow.com/questions/12336081/nohandlererror-with-rails3-and-paperclip?lq=1假設您的問題不是太本地化,檢查您的回形針設置通過運行其他集成它的項目,如https://github.com/tors/jquery-fileupload-rails-paperclip-example – prusswan

回答

1

鐵路的UJS不知道如何遠程提交多部分表單。從表單標記中刪除data-remote="true"

如果表單是通過ajax發送的,那麼很可能是它沒有被正確編碼,除非你知道你正在使用JavaScript中的FileData API。您可以使用XHR Level 2和FormData正確編碼多部分表單。在對其進行編碼之前,您必須使用FileData讀取文件內容。

+0

我做了,問題依然存在。 –

+0

打開檢查器中的網絡面板,查看錶單提交請求。從那裏你可以驗證它是作爲多部分發送的。查看請求的標題。如果它是通過ajax發送的,那麼很可能是它沒有被正確編碼,除非你知道你正在使用來自JavaScript的FileData api。 – leafo

+0

因此,看起來我無法將表單設置爲無關緊要。問題是Backbone.save使用Backbone.sync,它使用jQuery.Ajax,它不支持文件上傳。 –

相關問題