This is going to be a relatively short post, mainly because I have more I want to write about - but it all deservers its own write up, after I've done a bit more work with it. However today I will quickly go over a simple file uploading model and how to use.
For us newbs I am going to be as thourough as possible with this somewhat simple task. First make certain you have a model named upload (thus you would have a table called uploads, where you would store all sorts of info about your files). Inside your model put the following code:
def savefile(upfile, directory, savefile)
if upfile.path
FileUtils.mv(upfile.path, Dir.getwd << directory << savefile)
end
end
This over-simplified method takes 3 arguments. First is the file that was uploaded (through a POST transfer, via your multipart HTML form). Second argument is the directory that you want to save the file, relative to your public document root. Third is the file name you want to save this as.
In this simple setup we are not inserting anything into the database or even checking for the type of file - instead I leave that up to you. The only check I do is for:
if upfile.path
That makes certain there is a file (not some randomly entered text in the file field). If you fail to do this you may get this error when you submit a faulty filepath:
You have a nil object when you didn't expect it!
The error occured while evaluating nil.to_str
Now all you need to do is add the following to the target method in your controller:
if params[:file]
myfile = Upload.new
myfile.savefile(params[:file], "/upload-dir/", "thetest.gif")
end
This snippet assumes that the field you are using to upload your file has name = 'file'. Thats it, you should be able to upload files (as long as your directory is write enabled).
Next I thought I would take a second to share a simple mod you can make to the error pages for action_controller exception messages (this is great for your testing environment.. especially if you are like me and see these often).
On my machine the file to edit was hidden away in:
/usr/lib/ruby/gems/1.8/gems ...
/actionpack-1.11.2/lib/action_controller/ ...
templates/rescues/layout.rhtml
Its nice to change the color around a bit and add your own touches to that drab white page.
Well, thats it for this short post. Theres more to learn and more to write about, so - I'll be back here soon enough!

Charles said:
For a demonstration of the modified action_controller rescue layout.rhtml go here:
http://www.forthecode.com/not-a-real-action/
Since I have no method in my controller to handle that - it will give you the prettier Rails routing error.
2006-05-21 12:00:44 UTCJim said:
def savefile(upfile, directory, savefile)
if upfile.path
FileUtils.mv(upfile.path, Dir.getwd << directory << savefile)
end
end
When it comes to file methods fail - you may want to throw in a begin ~ rescue ~ ensure ~ end statement
like
def savefile(upfile, directory, savefile)
if upfile.path
2006-05-23 20:57:16 UTCbegin
FileUtils.mv(upfile.path, Dir.getwd << directory << savefile)
rescue
flash[:notice] = "File could not be moved"
ensure
#do something that needs to be done to whether it fails or not
end#error catching
end#if
end#save file
Charles said:
Interesting... didnt know much about begin - rescue, looks like gold to me!
Thanks
2006-05-23 23:09:01 UTC