Paperclip异常:Paperclip :: AdapterRegistry :: NoHandlerError

Ste*_*zin 40 ruby-on-rails paperclip

在rails 3.2.2中使用Paperclip 3.0.1我收到此错误:

**Paperclip::AdapterRegistry::NoHandlerError** 
(No handler found for "2009-11-29-133527.jpg"):
Run Code Online (Sandbox Code Playgroud)

在我的模型中,我有:

class Product < ActiveRecord::Base
    ...
    has_many :assets 
    accepts_nested_attributes_for :assets
 end

 class Asset < ActiveRecord::Base
     belongs_to :product
     has_attached_file :image,
               :path => ":rails_root/public/system/:attachment/:id/:style/:filename",
               :url => "/system/:attachment/:id/:style/:filename", 
               :styles => { :medium => "300x300>", :thumb => "100x100>" }
  end
Run Code Online (Sandbox Code Playgroud)

例外是在:

def create
     **@product = Product.new params[:product]**
     ...
end
Run Code Online (Sandbox Code Playgroud)

与params:

{...,
 "product"=>{"title"=>"wibble1", 
             **"assets_attributes"=>{"0"=>{"image"=>"2009-11-29-133527.jpg"}
                                  },** 
             "description"=>"Who is wibble...", 
             "price"=>"23.45"
            }, 
             "commit"=>"Create Product", 
             ...}
Run Code Online (Sandbox Code Playgroud)

有谁知道发生了什么?

Mau*_*uan 47

引发此错误是因为您没有为Paperclip提供正确的类.它只是一个字符串.

你应该收到这样的东西 params

"asset"=>
  {"image"=>
    #<ActionDispatch::Http::UploadedFile:0x000000056679e8
    @content_type="image/jpg",
    @headers= "Content-Disposition: form-data; name=\"asset[image]\";
      filename=\"2009-11-29-133527.jpg\"\r\nContent-Type: image/jpg\r\n",
    @original_filename=""2009-11-29-133527.jpg"",
    @tempfile=#<File:/tmp/RackMultipart20120619-1043-yvc9ox>>}
Run Code Online (Sandbox Code Playgroud)

在yout View中你应该有这样的东西(在HAML中,非常简化):

= form_for @product, html: { multipart: true } do |f|
  = f.fields_for :asset do |asset_form|
    = asset_form.file_field :image
Run Code Online (Sandbox Code Playgroud)

请记住将表单设置为multipart: true.

  • 是的,请记住设置multipart选项.例如:<%= form_for @video,:method =>:put,:html => {:multipart => true} do%> (2认同)

Ken*_*ika 27

我自己也遇到了这个问题.在我的情况下,它是由跳过标记中的multipart表单声明引起的.

我使用的是formtastic所以我添加了这个并使其工作:

semantic_form_for @picture, :html => {:multipart => true} do |f|

  • 对于未来的googlers,如果您正在使用form_for并在构建器之外添加字段,则必须通过`:html => {:multipart => true}来手动强制form_for为多部分,如上所示.简单地添加`:multipart => true`不会像使用form_tag那样工作 (4认同)

bjm*_*m88 5

请注意,使用值得注意的HTML5画布时会出现这种情况.将画布数据作为DataURI字符串并将其发送到服务器可能会导致此错误.Canvas .toDataURL()将提供类似"data:image/png; base64,iVBORw0KGg ..."的内容,您可以将其与其他信息一起发送到服务器,因此它与标准的多部分表单上传不同.在服务器端,如果您只是将其设置为回形针附件字段,您将收到此错误.您需要将其转换为文件或IO对象.你可以像这样写一个临时文件:

data_uri = params[:canvasDataUri]
encoded_image = data_uri.split(",")[1]
decoded_image = Base64.decode64(encoded_image)
File.open("signature.png", "wb") { |f| f.write(decoded_image) }
Run Code Online (Sandbox Code Playgroud)

或者使用Ruby的StringIO,它就像一个内存文件接口

@docHolder.document = StringIO.new(decoded_image)
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助.


Joe*_*ers 0

确保在安装 Paperclip 后迁移数据库(“rake db:migrate”)...此外,您可能需要将 Paperclip 生成的新数据字段添加到模型中的“attr_accessible”行。当我试图让回形针在我的一个项目中工作时,我遇到了类似的问题。