Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Multipart form with empty file_field submits nil

#1945

I noticed that posting a multipart form with a blank file_field submits a nil in edge rails. Is this the intended behavior? In rails 2.2 it submitted an empty string.

I'm using the paperclip plugin which ignores the empty string but deletes attachments that are set to nil. With this new behavior, paperclip deletes every attachment on update. If this is the intended behavior for rails 2.3 then I'll submit a ticket on the paperclip project about modifying the plugin to handle nil values a different way.

My rails SHA-1 is 1a2a71333f197e98d1cd7fe380d613cbada4446b

Reported by Chris Barnes · February 11th, 2009 @ 07:54 PM

State: resolved
Milestone: none
Assigned to: josh josh
Importance: none

Activity

  1. Chris Barnes
    Chris Barnes
    • Assigned user set to josh

    February 23rd, 2009 @ 06:43 PM

  2. josh
  3. josh
    josh
    • State changed from new to wontfix

    Yeah, it looks like rack's multipart parser sets it to nil, which is what we are using now. I think the behavior seems correct. If there is a a good reason to have blank uploads set to "", we should get this fixed in rack core.

    I'll make a note of it as an API change.

    February 24th, 2009 @ 05:47 PM

  4. PJ Kelly
    PJ Kelly

    So, just to clarify, is this something that needs to be changed in Rack, or in the behavior of plugins like Paperclip?

    March 5th, 2009 @ 09:01 AM

  5. deleted
    deleted

    it was not fixed in 2.3RC2

    March 9th, 2009 @ 12:05 PM

  6. Dmitry Polushkin
    Dmitry Polushkin

    Yeah, still doing:

    
    params[:hinge].delete(:image) unless params[:hinge][:image]
    

    before

    
    @hinge.update_attributes(params[:hinge])
    

    Should it be fixed in paperclip?

    March 11th, 2009 @ 01:13 AM

  7. Dmitry Polushkin
  8. Jon Yurek
    Jon Yurek

    I don't think "" is a correct value, but setting it to nil is problematic as file fields can't, unlike any other fields, hold a prepopulated value. So while other fields can simply set themselves to themselves, file fields set to nil will overwrite. While I think nil is technically correct in the case of HTML form submission, it's not in the case of acting like an attribute.

    Perhaps the best case is for the Rack multipart handler to simply leave the file out of the params if there is no upload, similarly to how unchecked checkboxes are left out.

    March 12th, 2009 @ 07:12 PM

  9. Rob Anderton
    Rob Anderton

    fwiw I'd agree that excluding file fields with no upload from params makes more sense than leaving them in and set to nil.

    March 12th, 2009 @ 09:53 PM

  10. josh
    josh

    Please submit a patch to rack core ASAP!

    March 13th, 2009 @ 02:55 AM

  11. Trevor Turk
    Trevor Turk

    I'm attaching a patch that sets blank uploads to "" instead of nil. It appears to fix the problems with Paperclip in my testing.

    I can't figure out how to ignore the attribute altogether. Maybe somebody else can give it a shot...?

    March 13th, 2009 @ 07:08 AM

  12. Trevor Turk
  13. Rob Anderton
    Rob Anderton

    I've submitted a slightly different patch over on the rack lighthouse that removes the empty file param instead of setting it to nil/blank.

    I'll post a note on the rack mailing list too...

    March 13th, 2009 @ 09:18 AM

  14. Trevor Turk
    Trevor Turk

    Rob, your patch works for me. Thank you!

    March 13th, 2009 @ 04:00 PM

  15. josh
    josh
    • State changed from wontfix to open

    March 13th, 2009 @ 10:08 PM

  16. Repository
    Repository
    • State changed from open to resolved

    (from [eced3d8c55c1bbae6915d7c6523c341bc682509a]) Update rack to fix multipart uploads with an empty file [#1945 Multipart form with empty file_field submits nil state:resolved] http://github.com/rails/rails/co...

    March 13th, 2009 @ 11:14 PM