For attaching files, use Paperclip
Update: There was a bug or two that cropped up overnight. Thanks to those in the comments that spotted them. You can piston update if you’re on trunk, or you can svn export the 2.0.1 release tag to get those fixed.
Update 2: Due to fixing a bug in url generation, I’ve tagged 2.0.2.
Update 3: I’ve created a Google Group for Paperclip as well as a Lighthouse, so we don’t need to be discussing issues in the comments anymore.
For some reason, file attachment is annoying. I don’t know why, and I know a lot of people have attempted to solve the problem in the past, myself included. Yet it still is. Having gotten fed up with gotchas and design decisions that we didn’t agree with, I went and wrote Paperclip on the plane to RailsConf last year. We’ve been using it here in various forms since and IMHO it’s the way to handle uploads, and finally decided that it should be released.
1 2 3 4 5 class User < ActiveRecord::Base has_attached_file :avatar, :styles => { :square => ["64x64#", :png], :small => "150x150>" } endA file is treated like any other attribute. It’s assigned like any other attribute, and it’s not saved until you call #save. It doesn’t have its own model. You can say where it’s saved on the filesystem, and what URL it’s referred to by (which means you can let Apache/nginx handle it or you can route it through the app for permissions/security). You can say what thumbnails are made, what resolution and format they are, and you can actually save cropped square thumbnails without any hassle.
...








