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.megabytesto
:size => 0.megabyte..1.megabytesMike 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 usingattachment_fuon 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 usingattachment_fuwith a RESTful model on Unix, it’s rare. Add this to the body of theApplicationControllerclass inapplication.rb:
def default_url_options(options)
{ :trailing_slash => true }
endAlso, 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.htmlUnfortunately, 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

Chris said
I was running into the same problem that you were with the return results using attachement_fu along with Khamsouk’s walkthrough while I was testing the REST aspect of it.
To fix it, just change uploaded_data to asset[uploaded_data] in your curl call where asset is the model name that you’re using.
Cheers,
C
Chirag Patel said
Thanks Chris! I used the following cURL command instead and it worked
curl -F "raw_data_file[uploaded_data]=@curltest.docx" http://localhost:3000/raw_data_files/create > results.html
Latest Bookmarks on Ma.gnolia.com at Ivan Enviroman said
[...] Ruby on Rails File Upload plug-ins « So far, it’s RoR [...]
Rails and file uploads « 41 technologies said
[...] and file uploads While there certainly are several good file upload plugins for Ruby on Rails. It is actually somewhat hard to find any good tutorials on how to do it yourself. I just wanted to [...]
chester said
step by step process from the start.
i’m a newby, pls.
thanks.
Manikandan said
I want to Know, how to get the uploaded file format eg..(.zip or .jpg ,etc,…)