So far, it\’s RoR

Ruby on Rails, PostgreSQL, SVN, etc….

Archive for June 28th, 2007

Ruby on Rails File Upload plug-ins

Posted by Chirag Patel on June 28, 2007

Basically, I dug up 5 plug-ins (plugins) that can do the job

1. acts as attachment

2. attachment_fu (extension of act_as_attachment)

I used Khamsouk’s tutorial (but without the AJAX) and had to change the following line from

:max_size => 1.megabytes


:size => 0.megabyte..1.megabytes

Mike Clark also has a good tutorial, but he doesn’t use the scaffold_resource generator (a good way to automatically generate REST-enabled models). Mike codes the model (that reperesent the uploaded file) and controller manually and only includes the POST action. Since Khamsouk’s tutorial uses scaffold_resource, you get POST, DELETE, GET, and PUT. Also with Khamsouk’s tutorial, you get some AJAX support (which I haven’t tried) and a little more customization with the view.

Also, I had a lingering problem with using attachment_fu on my hosted ‘production’ server. When I transferred the code from Windows to Textdrive (Solaris), it stopped working. I was not able to upload a file and no exception would occur. After some debugging and logging, I noticed that the ‘create’ action was not being registered by Rails. After submitting the file upload, the ‘new’ action would jump straight to the ‘index’ action, which is a display list of the uploaded files. Thanks to Steve Eichert‘ June 20, 2007 blog entry, I found a workaround. Since this problem only occurs when using attachment_fu with a RESTful model on Unix, it’s rare. Add this to the body of the ApplicationController class in application.rb :

def default_url_options(options)
{ :trailing_slash => true }

Also, I used cURL to test the RESTfulness of attachment_fu (ability to POST a file outside of a web form) using the –F switch:

curl -F "uploaded_data=@curltest.docx" http://localhost:3000/raw_data_files/create > results.html

Unfortunately, results.html shows the follwing errors
There were problems with the following fields:

* Content type can’t be blank

* Size is not included in the list

* Size can’t be blank

* Filename can’t be blank

3. File Column

4. Flex Image

5. UploadColumn

Here’s a pretty good write up of #1, #3, and #4. The other two (#2 and #5) are mentioned in the comments. One guys says this about UploadColumn vs. attachment_fu

“After using all the others (except fleximage) UploadColumn is my favourite. Much better documentation than attachment_fu, very simple to use, a lot like filecolumn but with a much more elegant way of retrieving image urls for your views.

I’m not uploading images, so I went with attachment_fu, mainly because compatibility with restful_authentification (which I plan on using later) looks promising.

Last but no least, Rubynoob wrote a really good blog entry on how to use form_for helper so that an HTML form (to upload a file, in this case) is automatically created from a model

Posted in Ruby on Rails | 6 Comments »