Caution! - Many of these posts are creepy-old in the Ruby on Rails world (before 1.0!)
The :author => Charles Abbott now blogs here

FileUtils, action_controller rescues

2006-05-20   [ 3 comments ]

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!

:author => "Charles Abbott"
Converting to Ruby on Rails
 


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 UTC

Jim 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
begin
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

2006-05-23 20:57:16 UTC

Charles said:

Interesting... didnt know much about begin - rescue, looks like gold to me!

Thanks

2006-05-23 23:09:01 UTC

What?

Who?              Link?



Frameworks Good or Bad?   :date => "2007-10-06"
Where is ForTheCode.com Going?   :date => "2007-09-23"
Refactoring - Vital to Software Development   :date => "2007-09-23"
Mongrel Cluster a quick note - and extra notes   :date => "2007-05-20"
Linux Mongrel and Rails   :date => "2007-05-15"
form_remote_tag revisited   :date => "2007-01-07"
How To: Ubuntu 6.10 Edgy on Rails part 3   :date => "2006-12-30"
How To: Ubuntu 6.10 Edgy on Rails part 2   :date => "2006-12-24"
How To: Ubuntu 6.10 Edgy on Rails   :date => "2006-12-22"
verify ... 5.times do cycle   :date => "2006-09-25"
country_select, country_options_for_select, mail_to   :date => "2006-09-05"
Generate and Send Email in Rails   :date => "2006-08-26"
FDF Model, gsub, and send_data   :date => "2006-08-18"
Active Directory Authentication with acts_as_authenticated   :date => "2006-08-17"
Apache2 proxy with Lighttpd - FastCGI for Rails   :date => "2006-08-08"
reverse! && a simple file Upload Class   :date => "2006-07-29"
send_file - a link to download a file   :date => "2006-07-24"
Environments (production, development, test) and cache_pages   :date => "2006-07-04"
.class .methods .instance_variables   :date => "2006-06-14"
select_tag :multiple => true   :date => "2006-06-01"
FileUtils, action_controller rescues   :date => "2006-05-20"
file_field_tag, File.size, File.path, FileUtils.mv   :date => "2006-05-15"
javascript_include_tag, stylesheet_link_tag   :date => "2006-05-02"
submit_to_remote, form_remote_tag, script.aculo.us   :date => "2006-04-30"
periodically_call_remote, simple_format   :date => "2006-04-26"
observe_field - Ajax!   :date => "2006-04-21"
h method, TextHelper, sanitize(), strip_tags()   :date => "2006-04-15"
Rails API :My API [.count(), link_to, text_area :size]   :date => "2006-04-13"
Rails - HTML Select Tag   :date => "2006-04-05"
Pruning Old Sessions   :date => "2006-03-21"
If Elsif Else, and Searching Too!   :date => "2006-03-17"
SHA1 - A quick update   :date => "2006-03-15"
Initialized! good, Authorized? Great! part 2   :date => "2006-03-11"
Initialized! good, Authorized? Great!   :date => "2006-03-08"
Forms and Routing in RoR   :date => "2006-03-06"
My First RoR Post !   :date => "2006-03-05"