Monday, October 29, 2012

attachment_fu as a gem for Rails 3.2

As I've mentioned before, I develop the web site for St. Francis Society Animal Rescue.  As I've also described in that earlier article, it started as a Rails 1.2 app, then Rails 2.1, then 2.3.  Right now it's in Rails 3.1, but I'm about to switch it to Rails 3.2.  Having some history, it was developed to use attachment_fu to upload all the images for the cats and dogs.  While I know many people have abandoned attachment_fu for paperclip, or carrierwave, or dragonfly, etc. attachment_fu has continued to work just fine for me so if it ain't broke, don't fix it.  Plus, there are currently over 13,000 animal images that have been uploaded, and I don't really want to hassle with converting them over to a new attachment system.

Enter Ruby 1.9.x

The last official update to the attachment_fu github repository was on April 25, 2009.  While that update is for Ruby 1.9 compatibility fixes, if you try to use it as is for Ruby 1.9.3, you'll find it won't work.  To that end, I've forked off a new repository that will fix additional Ruby 1.9.3 incompatibilities.  If you want to continue to use attachment_fu as a plugin with Ruby 1.9.x and Rails prior to 3.2, you can use my github repository in your project simply by issuing this command in your project:

git submodule add vendor/plugins/attachment_fu

Enter Rails 3.2.x

attachment_fu has always functioned as a plugin (vendor/plugins), the problem is that Rails 3.2 will give you this error if you continue to use it as a plugin:

DEPRECATION WARNING: You have Rails 2.3-style plugins in vendor/plugins! Support for these plugins will be removed in Rails 4.0. 
Move them out and bundle them in your Gemfile, or fold them in to your app as lib/myplugin/* and config/initializers/myplugin.rb. 
See the release notes for more on this:

To that end, I've also updated my fork of attachment_fu to function as a gem! Simply add this line to your Gemfile
gem 'pothoven-attachment_fu'

I need to acknowledge Christophe Porteneuve for doing most of the gem conversion work. I just pulled in his updates and fixed a few problems with it that I encountered. Please feel free to let me know if you have any problems using the gem.


that_robot said...

I'm working on bringing a website up from rails 2.3.6 to rails 4 and i originally wrote the site to use attachment_fu, so thank god for your updates!

It appears that I now have some routing problems with existing images though ("No route matches [GET]"). I'm not sure if it has to do with the new Asset pipeline, or if I need to do something in my routes file to get access to my image folder.

Is this a problem you ran into?

All my uploaded images are in public/attachments. Neither my original routes.rb or the new one reference this attachment folder, so I'm not sure exactly what I should change at this time.

Thanks for any tips!

that_robot said...

just kidding, it appears to work. tired from staring at code all day. Have a good day!

Anonymous said...

I am also getting the no route matches [GET] for attached images. My images get stored in public/images.

Did you have to do anything special to get it working that_robot? I have also updated a major application from 2.3 to 3.2 (and maybe 4 in the near future)

Unknown said...

Hi, can you share "attachment_fu as a gem for Rails 3.0.6"

Steven Pothoven said...

Did you try it with 3.0.6? It should work with back-level versions. What error are you getting?

Praaveen said...

hi steven,
im getting this error

"undefined method
`after_attachment_saved' for

Steven Pothoven said...

I presume this is due to my prior commenting out of lines 201-203 of lib/technoweenie/attachment_fu.rb when I was fixing it to work with Ruby 1.9 and Rails 3. See: attachment_fu.rb#L201-L203

I don't recall why I needed to do that at this time. Are you able to pull a copy of the repository, uncomment these lines and see if that works for you? If that fixes it for you, I'll determine what the impacts are toward leaving them uncommented (and what additional conditions will need to be wrapped around it).