paperclip No handler found for “file.jpg” rails 4

2019-07-24 06:44发布

问题:

I'm having problem with setting up paperclip gem in one of me apps.
I get No handler found for error message when i try to save uploaded file.

Things that I've done

  • migration:

    add_attachment :orders, :file  
    
  • model:

    has_attached_file :file  
    validates_attachment_content_type :file, :content_type => /\Aimage\/.*\Z/
    
  • controller:

    def order_params  
      params.require(:order).permit(:file)
    
  • view:

    form_tag url, method: :post, html: {multipart: true}  
    ...  
    = file_field_tag 'order[file]', disabled: true, id: 'mtd_file',accept: 'image/png,image/gif,image/jpeg'
    

When I try to execute in my controller Order.create params[order] I get error

No handler found for "file.jpg"

When I look into order[file] param I see that it's a string file.jpg (name of uploaded file).

回答1:

Try using the below line of code, you don't have to pass mulitpart as html hash

form_tag url, method: :post, multipart: true

For form_for you have to pass multipart in html hash

form_for(@user), :html => { :multipart => true } do |f|

For more info, you can refer to the documentation: RailsGuides - Form Helpers - 5 Uploading Files