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

时光毁灭记忆、已成空白 提交于 2019-12-11 02:35:18

问题


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



来源:https://stackoverflow.com/questions/29094795/paperclip-no-handler-found-for-file-jpg-rails-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!