回形针文件字段有时不起作用



我正在开发一个Rails应用程序,该应用程序使用Paperclip在模型中保存图片。为了保存这张图片,我有一个form_for,它使用文件输入来保存图片。我上传照片没有遇到任何问题,但有时它不起作用。大多数时候我都会收到这样的参数:

"picture"=>#<ActionDispatch::Http::UploadedFile:0x000000061faeb8 
  @original_filename="sample8.jpg", @content_type="image/jpeg",
  @headers="Content-Disposition: form-data; name="service[picture]"; 
  filename="sample.jpg"rnContent-Type: image/jpegrn",
  @tempfile=#<File:/tmp/RackMultipart20141026-3030-s39mpm>>, 

有了这种类型的参数,我可以上传图片。然而,有时我会收到这样的参数:"picture"=>"sample.jpg",所以我会得到以下错误:

No handler found for "sample.jpg".

我一直在网上冲浪,试图找到一个类似的问题,但我什么也没找到。有人知道为什么会发生这种事吗?

这是我使用的主要文件:

= form_for service, url: service_path( service ), html: { method: :put } do |f|
  = file_field_tag :picture, value: service.picture,
    id: "selected_file_service_#{ service.id }", class: 'selected_file_service',
    size: '24', name: "service[picture]", rel: service.id
  %input#browseButton.upload_picture.service_upload.service_picture_upload{
    type: "button", value: I18n.t( 'abadi.actions.upload_picture' ) }

终于找到了解决方案。我需要在html参数的表单中使用multipart:true

最新更新