Paperclip exception : Paperclip::AdapterRegistry::NoHandlerError
Asked Answered
S

11

44

Using Paperclip 3.0.1 in rails 3.2.2 I got this error:

**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?

Shagbark answered 5/4, 2012 at 17:50 Comment(0)
E
50

This Error is raised because you aren't giving Paperclip a correct class. It's a just a String.

You should receive something like this in 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):

= 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.

Emelina answered 19/6, 2012 at 19:46 Comment(4)
Yes do remember to set the multipart option. For example : <%= form_for @video, :method => :put, :html => { :multipart => true} do %>Finally
In case of form_tag it should be form_tag path, :multipart => true without :html key. LinkLenticularis
What in case I am selecting image from assets.Dessertspoon
@Imran How are you selecting it? It sounds like a different question reallyEmelina
P
30

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:

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

Pinpoint answered 9/4, 2012 at 16:7 Comment(2)
For future googlers, if you're using form_for and adding fields outside of the builder, you have to manually force form_for to be multipart, via :html => {:multipart => true} as shown above. Simply adding :multipart => true doesn't work like it does with form_tagInstrumentalist
This answer worked for me! I hope the asker would accept this one. This is usually the case that happens when you create a form for a model, then eventually have nested attributes for sub objects that accepts files as attachments which, in my case, was the scenario. (relating my story here for future googlers)Mossman
K
5

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:

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

@docHolder.document = StringIO.new(decoded_image)

Hope this helps.

Kamikamikaze answered 8/7, 2014 at 3:38 Comment(0)
P
2

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.

Praseodymium answered 31/10, 2012 at 16:10 Comment(3)
This was my problem, as well. I liked the convenience of :multiple => true, but as you say, it sets the name as upload[upload][]. However, I really like the convenience of multiple file selection, so I just set the name manually to not be an array with :name => 'upload[upload]'.Hughmanick
I add multiple with javascript together with jquery fileupload ;)Praseodymium
Hah! Almost three years later, and I ran into exactly this on another project, and as before, this answer and my comment solved my problem.Hughmanick
H
2

my problem was not accepting get method in routes so i changed it as patch method and it work fine.

<%= form_for @product, :url => "/products/#{@product.id}/upload",:method => :patch, :html => { :multipart => true } do |f| %>
Hypersensitize answered 11/3, 2016 at 10:8 Comment(0)
A
1

I'm pretty sure your problem is with the form_for in the view, try something like this:

<%= 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 %>
Andreeandrei answered 1/1, 2014 at 14:33 Comment(0)
C
1

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.

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:

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
Coverlet answered 25/2, 2017 at 19:15 Comment(0)
S
0

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.

Scant answered 5/4, 2012 at 17:56 Comment(1)
unfortunately, this doesn't solve the problem for me. Same problem adding attr_accessible :title,:description,:price,:assets_attributes,Shagbark
T
0

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 :photo column 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/:id path, if you have two columns both named as photo, then the problem occurs I think.

Tragedienne answered 14/4, 2012 at 7:37 Comment(0)
K
0

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)
Kati answered 12/4, 2015 at 11:7 Comment(0)
E
0

When we upgraded from 4.2.x to 4.3.x, we had to change the main paperclip field attribute (picture, image, etc) from a relative URL to a full URL to eliminate this error.

Euphonic answered 20/2, 2018 at 16:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.