Ruby-on-rails 回形针异常:回形针::AdapterRegistry::NoHandlerError
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10033425/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Paperclip exception : Paperclip::AdapterRegistry::NoHandlerError
提问by Sten Ka Razin
Using Paperclip 3.0.1 in rails 3.2.2 I got this error:
在 rails 3.2.2 中使用 Paperclip 3.0.1 我得到这个错误:
**Paperclip::AdapterRegistry::NoHandlerError**
(No handler found for "2009-11-29-133527.jpg"):
In my model I have:
在我的模型中,我有:
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
The exception is raised at:
在以下位置引发异常:
def create
**@product = Product.new params[:product]**
...
end
with params:
带参数:
{...,
"product"=>{"title"=>"wibble1",
**"assets_attributes"=>{"0"=>{"image"=>"2009-11-29-133527.jpg"}
},**
"description"=>"Who is wibble...",
"price"=>"23.45"
},
"commit"=>"Create Product",
...}
Anyone know what's going on?
有谁知道这是怎么回事?
回答by Mauricio Pasquier Juan
This Error is raised because you aren't giving Paperclip a correct class. It's a just a String.
引发此错误是因为您没有为 Paperclip 提供正确的类。它只是一个字符串。
You should receive something like this in params
你应该收到这样的东西 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>>}
And you should have something like this in yout View (in HAML, very simplified):
你应该在你的视图中有这样的东西(在 HAML 中,非常简化):
= form_for @product, html: { multipart: true } do |f|
= f.fields_for :asset do |asset_form|
= asset_form.file_field :image
Remember to set your form to multipart: true.
请记住将您的表单设置为multipart: true.
回答by Ken Mazaika
I just ran into this problem myself. In my case it was caused by skipping the multipart form declaration in the markup.
我自己刚刚遇到了这个问题。在我的情况下,它是由跳过标记中的多部分表单声明引起的。
I was using formtastic so I added this and got it working:
我正在使用 formtastic 所以我添加了这个并让它工作:
semantic_form_for @picture, :html => {:multipart => true} do |f|
semantic_form_for @picture, :html => {:multipart => true} do |f|
回答by bjm88
Note there is a situation when working with an HTML5 canvas that is worth noting. Getting the canvas data as a DataURI string and sending that to server can cause this error. Canvas .toDataURL() will give something like "data:image/png;base64,iVBORw0KGg..." which you can send to server with other information, so its different than a standard multi-part form upload. On the server side if you just set this to the paperclip attachment field you will get this error. You need to conver it to a file or IO object. You could write a temp file like this:
请注意,在使用 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) }
or use Ruby's StringIO which acts like an in memory file interface
或使用 Ruby 的 StringIO,它的作用类似于内存文件接口
@docHolder.document = StringIO.new(decoded_image)
Hope this helps.
希望这可以帮助。
回答by Mirko
I had <input type="file" ... multiple="multiple">on file input, so paperclip attachment data was in an array.
I solved this simply by removing multiple attribute on file input.
我有<input type="file" ... multiple="multiple">文件输入,所以回形针附件数据在一个数组中。我只是通过删除文件输入上的多个属性来解决这个问题。
回答by Manoranjan
my problem was not accepting get method in routes so i changed it as patch method and it work fine.
我的问题是不接受路由中的 get 方法,所以我将其更改为补丁方法并且它工作正常。
<%= form_for @product, :url => "/products/#{@product.id}/upload",:method => :patch, :html => { :multipart => true } do |f| %>
回答by Julio Marins
I'm pretty sure your problem is with the form_for in the view, try something like this:
我很确定您的问题出在视图中的 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 %>
回答by Neon_10
for me the problem was like this:
对我来说,问题是这样的:
I used such line in controller, as saw it in some answers:
我在控制器中使用了这样的行,正如在一些答案中看到的那样:
@image = User.find(params[:id]).image.<b>path</b>(:small)
and I had the problem "no handler for the file"
我遇到了“文件没有处理程序”的问题
so, I just removed "path" and it worked:
所以,我刚刚删除了“路径”并且它起作用了:
@image = User.find(params[:id]).image(:small)
回答by Yitong Zhou
I have met the same problem, I think it is because there are two tables sharing the same attached_file_name... In my case, I add a :photocolumn both to activities and tweets, then the case seems to be that the system can find one of them but not the other. Because the files are saved in /public/photo/:id/:idpath, if you have two columns both named as photo, then the problem occurs I think.
我遇到了同样的问题,我认为是因为有两个表共享相同的attached_file_name...在我的情况下,我:photo在活动和推文中都添加了一列,然后情况似乎是系统可以找到其中一个但不是另一个。因为文件保存在/public/photo/:id/:id路径中,如果您有两列都命名为photo,那么我认为会出现问题。
回答by Joe Saunders
Make sure you migrate the database after you install Paperclip ('rake db:migrate')... Also, you might need to add the new data fields generated by Paperclip to your 'attr_accessible' line in the model. I had a similar problem when I was trying to get Paperclip workin on one of my projects.
确保在安装 Paperclip ('rake db:migrate') 之后迁移数据库...此外,您可能需要将 Paperclip 生成的新数据字段添加到模型中的“attr_accessible”行。当我试图让 Paperclip 在我的一个项目中工作时,我遇到了类似的问题。
回答by ShadSterling
In my case I was passing String, as in @MauricioPasquierJuan's answer, but I wasn't using a form so the rest of the answer doesn't apply.
在我的情况下,我传递了字符串,就像@MauricioPasquierJuan 的答案一样,但我没有使用表格,所以其余的答案不适用。
I couldn't find any documentation of how to programatically update an attachment - what types can be assigned, and why assigning and saving the modified record does not save modified attachments. This question was the closest thing I found.
我找不到任何关于如何以编程方式更新附件的文档 - 可以分配哪些类型,以及为什么分配和保存修改后的记录不会保存修改后的附件。这个问题是我发现的最接近的问题。
Inside a function that process files inside an uploaded zip file, after saving extracted files to temp files, this is my solution:
在处理上传的 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

