简体   繁体   中英

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!

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

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!

Try my gemified version that supports Rails 3.2:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM