42

在 rails 3.2.2 中使用 Paperclip 3.0.1 我得到了这个错误:

**Paperclip::AdapterRegistry::NoHandlerError** 
(No handler found for "2009-11-29-133527.jpg"):

在我的模型中,我有:

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

异常在以下位置引发:

def create
     **@product = Product.new params[:product]**
     ...
end

带参数:

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

有谁知道发生了什么?

4

11 回答 11

49

引发此错误是因为您没有为 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>>}

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

= form_for @product, html: { multipart: true } do |f|
  = f.fields_for :asset do |asset_form|
    = asset_form.file_field :image

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

于 2012-06-19T19:46:25.167 回答
29

我自己也遇到了这个问题。在我的情况下,这是由于跳过标记中的多部分表单声明引起的。

我正在使用formtastic,所以我添加了这个并让它工作:

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

于 2012-04-09T16:07:55.593 回答
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) }

或使用 Ruby 的 StringIO,它的作用类似于内存文件接口

@docHolder.document = StringIO.new(decoded_image)

希望这可以帮助。

于 2014-07-08T03:38:14.210 回答
2

我有<input type="file" ... multiple="multiple">文件输入,所以回形针附件数据在一个数组中。我只是通过删除文件输入上的多个属性来解决这个问题。

于 2012-10-31T16:10:53.217 回答
2

我的问题是不接受路由中的 get 方法,所以我将其更改为补丁方法并且它工作正常。

<%= form_for @product, :url => "/products/#{@product.id}/upload",:method => :patch, :html => { :multipart => true } do |f| %>
于 2016-03-11T10:08:24.897 回答
1

我很确定您的问题出在视图中的 form_for 上,请尝试以下操作:

<%= form_for @restaurante, :html => { :multipart => true } do |form| %>
   Nome:<%= form.text_field :nome%>
   Endereço:<%= form.text_field :endereco %>
   Especialidade:<%= form.text_field :especialidade %>
   Foto:<%= form.file_field :foto %>
   <%= form.submit 'create'%>
<% end %>
于 2014-01-01T14:33:37.043 回答
0

我遇到了同样的问题,我认为是因为有两个表共享相同的attached_file_name......在我的情况下,我在:photo活动和推文中都添加了一列,那么情况似乎是系统可以找到其中一个但不是另一个。因为文件保存在/public/photo/:id/:id路径中,如果你有两列都命名为photo,那么我认为问题就出现了。

于 2012-04-14T07:37:06.690 回答
0

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

于 2012-04-05T17:56:20.650 回答
0

对我来说,问题是这样的:

我在控制器中使用了这样的行,正如在一些答案中看到的那样:

@image = User.find(params[:id]).image.<b>path</b>(:small)

我遇到了“文件没有处理程序”的问题

所以,我刚刚删除了“路径”并且它起作用了:

@image = User.find(params[:id]).image(:small)
于 2015-04-12T11:07:34.413 回答
0

就我而言,我传递了字符串,就像@MauricioPasquierJuan 的回答一样,但我没有使用表格,所以其余的答案不适用。

我找不到任何有关如何以编程方式更新附件的文档 - 可以分配哪些类型,以及为什么分配和保存修改后的记录不会保存修改后的附件。这个问题是我发现的最接近的问题。

在处理上传的 zip 文件中的文件的函数中,将提取的文件保存到临时文件后,这是我的解决方案:

record.attachment = File.new( tempfile_path_as_string ) ## probably not the only option
record.attachment.save                                  ## next line doesn't update attachment without this
record.save
于 2017-02-25T19:15:07.820 回答
0

当我们从 4.2.x 升级到 4.3.x 时,我们必须将主要的回形针字段属性(pictureimage等)从相对 URL 更改为完整 URL 以消除此错误。

于 2018-02-20T16:43:10.037 回答