Plugin: attachment_fu

Posted by topher
on Thursday, July 19
attachment_fu is a rails plugin written by Rick Olson. From the README,

attachment_fu facilitates file uploads in Ruby on Rails. There are a few storage options for the actual file data, but the plugin always at a minimum stores metadata for each file in the database.

Check out Mike Clark’s tutorial.

On windows, when I submit the form with the file upload, I get an error “Size not included in list”. Upon inspection, the size is zero. (The default minimum size is 1 byte.) The size of the file uploaded isn’t set properly. I found somewhere on the beast forum that you need to add “sleep 1” on your controller. I haven’t tried to use attachment_fu on my server (ubuntu) and I’m hoping this is only a problem with windows.

My friend Mon, also a rails developer, encountered another problem with attachment_fu. He added a user_id in the model which uses attachment_fu. When he saves the uploaded file, he gets an error saying that user_id is null. We’re absolutely sure there’s a user_id.

Here’s what happened: attachment_fu can create a thumbnail(s) for you when you save the uploaded image (you will need an image processor like image magick). If you don’t specify a thumbnail_class, attachment_fu will use the same model for the thumbnail. So if you have a Mugshot model, after submitting the form, 1 model will be created for the uploaded image, and another one will be created for the thumbnail. So even if you add user_id to your model, the thumbnail won’t have a user_id. You need to add a callback before_thumbnail_saved on your model.

1
2
3
before_thumbnail_saved do |record, thumbnail|
  thumbnail.user_id = record.user_id
end

Install the plugin with

ruby script/plugin install http://svn.techno-weenie.net/projects/plugins/attachment_fu/