Has anyone been able to get attachment_fu to work with rails 3?
Asked Answered
S

3

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!

Smitty answered 20/6, 2011 at 19:39 Comment(4)
Can you post the full stack trace? Possibly best as Gist or Pastie.Churrigueresque
Here's the stack trace as a gist: gist.github.com/1038499 It looks a lot like the trace in the other post about TypeError.Smitty
Were you able to figure this out?Tamar
deb, can you post your stack trace illustrating the same issue?Kozak
T
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

Tamar answered 5/8, 2011 at 15:15 Comment(1)
Thanks!! I'll be able to use it when my priorities let me get back to the rails3 port.Smitty
V
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!

Vocalism answered 7/7, 2011 at 10:9 Comment(2)
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: ruby.simapse.com/2011/03/…Smitty
Link to ruby.simapse.com/2011/03/… is broken.Volatile
F
3

Try my gemified version that supports Rails 3.2:

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

Finicky answered 13/3, 2013 at 16:57 Comment(2)
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!Smitty
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.Horsley

© 2022 - 2024 — McMap. All rights reserved.