FileColumn - easy handling of file uploads in Rails

Attention: I will be speaking at the Canada on Rails conference, which will be held on April 13th-14th in Vancouver, Canada. David Heinemeier-Hansson will be giving a keynote and there are a lot of other big names on the list of speakers. I'm honored to give a talk about file_column and developing plugins in general.


This library makes handling of uploaded files in Ruby on Rails as easy as it should be. It helps you to not repeat yourself and write the same file handling code all over the place while providing you with nice features like keeping uploads during form redisplays, nice looking URLs for your uploaded files and easy integration with RMagick to resize uploaded images and create thumb-nails. Files are stored in the filesystem and the filename in the database.

Example

As you can judge a library best by looking at how to use it, here is a short example:

Just make the "image" column ready for handling uploaded files...


    class Entry < ActiveRecord::Base
        file_column :image
    end
    

... generate file fields that keep uploaded images during form redisplays to your view...


    <%= file_column_field "entry", "image" %>
    

... and display uploaded images in your view:


    <%= image_tag url_for_file_column("entry", "image") %>
    

It's just as easy! Why should it be any more difficult for a Rails application?

So what about the RMagick integration? Have a look:

To resize every uploaded image to a maximum size of 640x480, you just have to declare an additional option.


    class Entry < ActiveRecord::Base
        file_column :image, :magick => { :geometry => "640x480>" }
    end
    

You can even automatically create versions in different sizes that have nice filenames...


    class Entry < ActiveRecord::Base
        file_column :image, :magick => { 
          :versions => { "thumb" => "50x50", "medium" => "640x480>" }
        }
    end
    

... and display them in your view:


     <%= image_tag url_for_file_column("entry", "image") %>
     

Features

Here is a short list of features:

  • Uploaded files are stored in the filesystem. For the example above, a uploaded file "test.png" in an entry object with id 42 would be stored at "public/entry/image/42/test.png".
  • Newly uploaded files are stored in a temporary directory first, so that they are available during form redisplays, even if the model object is not saved yet.
  • You can easily generate a file upload field in views that will keep the uploaded file during form redisplays.
  • A helper will generate the nice-looking URL where you can access the uploaded file.
  • Tries to auto-detect file-types and fix the extension if it is not set properly, so that files are served with the correct mime-type by the web-server.
  • Easy integration with RMagick to resize uploaded image and create multiple versions in different sizes.

Download

The project's subversion repository can be found at http://opensvn.csie.org/rails_file_column/plugins/file_column. Since some 0.14.x release, rails contains a plugin manager that you can use to install file_column as a plugin. Just type (all in one line)


  ./script/plugin install 
    http://opensvn.csie.org/rails_file_column/
    plugins/file_column/trunk
  

in your project's working directory to install the latest version into "vendor/plugins/file_column". You can find stable releases in the "tags" directory of the subversion repository.

You can download the newest and older versions here. Please have a look at the README and the rdoc-documentation.

License

FileColumn is licensed under the same license as Ruby on Rails (MIT).


Canada on Rails

[ Impressum ]