Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 404
Chris G. Avatar asked Jun 20 '11 19:06

Chris G.


2 Answers

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

like image 108
deb Avatar answered Sep 23 '22 07:09

deb


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!

like image 39
user681814 Avatar answered Sep 22 '22 07:09

user681814