7

I have a rails application that is being upgraded from Rails 2.3.5 to Rails 3. It uses attachment_fu for file uploads. We're trying to do this conversion without making DB changes, so I'd like to avoid changing to paperclip or carrierwave at this time.

Has anyone succeeded in using attachment_fu with Rails 3 and Ruby 1.9.2? We're using the most recent version of attachment_fu that claims to be ok for rails 3 and ruby 1.9.2, but getting 'TypeError (can't convert nil into Integer):' on any forms that include a file upload.

All the answers to previous questions seem to be 'just switch to paperclip or carrierwave' as in: Attachment_fu or Paperclip for Rails3 or TypeError (can't convert nil into Integer):

Thanks!

Community
  • 1
  • 1
Chris G.
  • 690
  • 6
  • 11

3 Answers3

7

I made the following changes and it worked

attachment_fu.rb

def temp_path
  p = temp_paths.first
  if p.is_a?(ActionDispatch::Http::UploadedFile) # Rails 3.0.3 compatability fix
    p.tempfile.path
  else
    p.respond_to?(:path) ? p.path : p.to_s
  end
end

I also changed returning filename.strip do |name| to filename.strip.tap do |name|

init.rb

def make_tmpname(basename, n)
  ext = nil
  n ||= 0
  sprintf("%s%d-%d%s", basename.to_s.gsub(/\.\w+$/) { |s| ext = s; '' }, $$, n, ext)
end

I made a fork on github with this changes https://github.com/debprado/attachment_fu

Gaurav Sharma
  • 477
  • 9
  • 24
deb
  • 12,326
  • 21
  • 67
  • 86
6

attachment_fu patches Tempfile.make_tmpname in attachment_fu/init.rb, and it doesn't work in 1.9.2: sprintf("%d",nil) fails, and in 1.8.7 the result of this expression is "0".

The fix is to insert a line in init.rb from:

sprintf('%s%d-%d%s', File::basename(basename, ext), $$, n, ext)

to

n ||= 0
sprintf('%s%d-%d%s', File::basename(basename, ext), $$, n, ext)

You can find some of the discussion here https://github.com/technoweenie/attachment_fu/issues/25

Cheers!

user681814
  • 799
  • 1
  • 8
  • 19
  • I haven't had a chance to try it since getting the response due to other priorities. We're also considering switching to carrierwave, but using the attachment_fu naming convention as described here: http://ruby.simapse.com/2011/03/migrate-attachmentfu-to-carrierwave.html – Chris G. Aug 01 '11 at 15:11
  • Link to http://ruby.simapse.com/2011/03/migrate-attachmentfu-to-carrierwave.html is broken. – oldfartdeveloper Apr 20 '16 at 22:58
3

Try my gemified version that supports Rails 3.2:

https://rubygems.org/gems/pothoven-attachment_fu

  • On my project we ended up switching to carrierwave and using the attachment_fu naming convention as described above. However, I'm sure that finding out there's a gemified version will be helpful to anyone that's still using attachment_fu. There were good reasons for us to switch to carrierwave in the end, but it wasn't painless. Thanks for the pointer - if you end up using this gem come back and +1 the answer! – Chris G. Mar 13 '13 at 19:33
  • I used this and it works perfectly for most cases. Although by now I think I may have forked it and am using my own hacked one already. – thekingoftruth Aug 29 '13 at 18:15