回形针异常:回形针::AdapterRegistry::NoHandlerError [英] Paperclip exception : Paperclip::AdapterRegistry::NoHandlerError

查看:63
本文介绍了回形针异常:回形针::AdapterRegistry::NoHandlerError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 rails 3.2.2 中使用 Paperclip 3.0.1 时出现此错误:

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"):

在我的模型中,我有:

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", 
             ...}

有人知道这是怎么回事吗?

Anyone know what's going on?

推荐答案

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

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

您应该在 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 中,非常简化):

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

记得将表单设置为 multipart: true.

这篇关于回形针异常:回形针::AdapterRegistry::NoHandlerError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆