Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 使用Rails、回形针和主干上传文件_Ruby On Rails_Ruby_Backbone.js_Paperclip - Fatal编程技术网

Ruby on rails 使用Rails、回形针和主干上传文件

Ruby on rails 使用Rails、回形针和主干上传文件,ruby-on-rails,ruby,backbone.js,paperclip,Ruby On Rails,Ruby,Backbone.js,Paperclip,我正在通过创建一个简单的imageboard来学习Rails。我希望用户能够上传图像到服务器,然后我将能够为他们服务 我用的是rails主干和回形针 以下是相关部分: app/models/image.rb class Image < ActiveRecord::Base attr_accessible :url attr_accessible :data has_attached_file :data, :styles => { :medium => "300x3

我正在通过创建一个简单的imageboard来学习Rails。我希望用户能够上传图像到服务器,然后我将能够为他们服务

我用的是rails主干和回形针

以下是相关部分:

app/models/image.rb

class Image < ActiveRecord::Base
  attr_accessible :url
  attr_accessible :data
  has_attached_file :data, :styles => { :medium => "300x300>", :thumb => "100x100>" }
end
我还运行了此迁移:

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

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

任何帮助都将不胜感激!谢谢

Rail的UJS不知道如何远程提交多部分表单。从表单标记中删除
data remote=“true”


如果表单是通过ajax发送的,那么很可能它没有被正确编码,除非您知道您正在使用来自JavaScript的filedataapi。可以使用XHR级别2和正确编码多部分表单。在对其进行编码之前,必须使用FileData读取文件内容。

可能的重复:似乎是相同的错误,只是解决方案似乎是告诉表单该表单是多部分的。我的表单有enctype=“multipart/form data”,所以我不认为这是问题所在。另一个问题:假设您的问题不是太本地化,请运行其他集成它的项目来检查您的回形针设置,就像我针对您提供的示例运行它一样,它工作正常。我不认为这是回形针本身——文件上传从未正确填充params字段。我这样做了,但问题仍然存在。打开inspector中的网络面板,查看表单提交请求。从那里,您可以验证它是否作为多部分发送。查看请求的标题。如果它是通过ajax发送的,那么很可能它没有被正确编码,除非您知道您使用的是来自JavaScript的FileData api。因此,我将表单设置为什么似乎无关紧要。问题是Backbone.save使用Backbone.sync,后者使用jQuery.Ajax,后者不支持文件上传。
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
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'