file_field throws exception when field is left blank

hi -

I'm adapting a file upload from the Agile book. I have two models,
Challenge and MediaFile. The form looks like this:

<%form_for :challenge, :url => {:action
=>:create}, :html=>{:multipart=>true} do |f|%>
<%fields_for :media_file do |m|%>
      Please select a media file: <%=m.file_field :uploaded_data%>

The MediaFile model has a method defined 'uploaded_data'

def uploaded_data=(picture_field)
    name = base_part_of(picture_field.original_filename)
    content_type = picture_field.content_type.chomp
    data =

This works fine, unless the user doesn't select a file for upload. In
this case, picture_field is a blank string, and the model has no
attribute original_filename, etc. So the exception is:

undefined local variable or method `original_filename' for #<MediaFile:

The question is, how do I detect when a user hasn't selected a file
for upload, and gracefully add an error to the errors collection? I
wonder if this has something to do with having two models? Like I
said, it works just fine when the user selects a file.

Thanks for any help,

Rescue the exception and add an error to the model.

  # put whatever explodes here
  @foo.errors.add( :file, 'oops' )