2012-10-31 58 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,它不支持文件上传。 –

相关问题