#19 new
Paul

If no file uploaded, attachment_fu blows up in backends when saving existing record

Reported by Paul | October 18th, 2007 @ 08:08 PM

I wanted a model with has_attachment (and no validation), but which doesn't require an attachment. There is no problem creating these models initially, but there is a problem when you go to save the model again.

With no uploaded_data supplied the set_size_from_temp_path function make a series of calls, which would end up in the backend's full_filename. In this function, thumbnail_name_for in the attachment_fu instance methods would be called which would return the filename and since it doesn't exist it returns nil. At some point the nil value would be converted to a string throwing the exception: can't convert nil into String. While, I am using the file system backend someone had a similar problem with the s3 backend.

I think the attached patch short cuts the whole problem, but you may want to check.

Comments and changes to this ticket

  • Paul

    Paul October 23rd, 2007 @ 05:13 PM

    • Assigned user set to “Rick”
  • Dov Murik

    Dov Murik November 20th, 2007 @ 11:38 AM

    I believe there's a problem also in @@@destroy@@@.

    I have an model with @@@has_attachment@@@ (no validation, doesn't require an attachment). I create such an object without attachment and save it. Then I try to destroy it -- bang! There's a can't convert nil to string exception in @@@full_filename@@@. I rewrote @@@full_filename@@@ to this and so far it works:

    def full_filename(thumbnail = nil)
      return nil if thumbnail_name_for(thumbnail).blank?
      file_system_path = (thumbnail ? thumbnail_class : self).attachment_options[:path_prefix].to_s
      File.join(RAILS_ROOT, file_system_path, *partitioned_path(thumbnail_name_for(thumbnail)))
    end
    

    (I actually I just added the first line in the method)

    Best,

    dubek.

  • pablobm

    pablobm December 17th, 2007 @ 07:43 PM

    I can confirm this bug still exists in both versions described above.

    A combination of the patch submitted by Paul + the one-liner proposed by dubek fixes the problem

  • Vapor

    Vapor February 6th, 2008 @ 08:47 AM

    This was really annoying!

  • nisguy_300 (at yahoo)

    nisguy_300 (at yahoo) February 6th, 2008 @ 10:03 AM

    What I did to get around this was create a separate table and model for the file attachments and using a "has_many :attachments, :dependent => :destroy" in my parent model. Doing it this way opened up the opportunity to have multiple attachments for any one record. I think I used http://railscasts.com/episodes/75 as a reference.

  • tekk

    tekk March 25th, 2008 @ 03:50 AM

    I found this ticket somewhat late and came up with another solution for the db backend: adding a backend-specific temp_paths method.

    http://pastie.caboo.se/170173

  • Alistair Holt

    Alistair Holt August 15th, 2008 @ 06:57 AM

    • Tag set to attachment_fu

    I also found this problem when using the file system backend. The patch works perfectly.

    Shame this hasn't made it into the trunk.

  • marek

    marek April 28th, 2009 @ 08:53 AM

    I know I'm late to the party, but just wanted to let everyone know that Dov Muriks patch works excellent for me. I put it in a separate file in config/initializers as a monkey patch:

    @@@ Technoweenie::AttachmentFu::Backends::FileSystemBackend.class_eval %( def full_filename(thumbnail = nil)

    return nil if thumbnail_name_for(thumbnail).blank?
    file_system_path = (thumbnail ? thumbnail_class : self).attachment_options[:path_prefix].to_s
    File.join(RAILS_ROOT, file_system_path, *partitioned_path(thumbnail_name_for(thumbnail)))
    
    

    end)

    
    
  • Sim

    Sim February 2nd, 2010 @ 05:56 AM

    I try to use Dov Muriks patch but it doesn't work. I can't download/open the file. Could someone please explain me how this work?

    Thanks

  • Dusan Maliarik

    Dusan Maliarik December 6th, 2010 @ 05:04 AM

    • Milestone order changed from “0” to “0”

    Actually both #destroy_file and #full_filename need to be patched. Anyway, attachment_fu is dead, so we should use next crappy "file field in a gem" called paperclip, just to make our lives a bit more difficult.

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

Attachments

Pages