Paperclip exception : Paperclip::AdapterRegistry::

2019-01-03 17:15发布

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?

11条回答
smile是对你的礼貌
2楼-- · 2019-01-03 17:56

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.

查看更多
冷血范
3楼-- · 2019-01-03 17:58

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|

查看更多
兄弟一词,经得起流年.
4楼-- · 2019-01-03 18:00

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.

查看更多
走好不送
5楼-- · 2019-01-03 18:01

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 %>
查看更多
欢心
6楼-- · 2019-01-03 18:04

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| %>
查看更多
冷血范
7楼-- · 2019-01-03 18:05

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)
查看更多
登录 后发表回答