File-uploads in rails 2.0.2 + AJAX


I am porting an app from 1.2.5 to 2.0.2 and I am having problems with file
uploads. The form fields look like this:

<%= 'Add attachment'.t %>: <%= file_field 'attachments', "upload#{id}", :size => '40' %>

The old code expects params[:attachments]["upload#{i}"] to be an object (File?)
with a method "original_filename".
I am using ajax (and previously didn't):

  <% form_remote_tag(:url => url_for(:controller => :mex, :action => :submit_answer, :id =>, :update => "answer_#{}", :html => {:multipart => mex_exercise.num_attachments_allowed > 0}) do %>

(num_attachments_allowed is > 0 and I see the enctype-attribute:
  <form action="/en/mex/submit_answer/46" enctype="multipart/form-data" method="post" onsubmit="new Ajax.Updater('answer_46', '/en/mex/submit_answer/46', {asynchronous:true, evalScripts:true, parameters:Form.serialize(this) + '&authenticity_token=' + encodeURIComponent('a5de291b429a9def0d4ff822d8e278172e472a28')}); return false;"><div style="margin: 0pt; padding: 0pt;">)

Do I need to change anything because of ajax?


File uploads can't be done over ajax (because the javascript isn't allowed to read files from your computer). There are tricks you can play with iframes and so on.


Indeed as another poster has already mentioned you can’t upload files through ajax (javascript security).

Either use a Flash uploader like (but if you’re not fairly fluent in Javascript, i would advise against it), do know that the new session security measures cause problems if you want the upload to be authenticated or search for the responds_to_parent plugin and follow the instructions.

Best regards

Peter De Berdt

I also use v2.0.2 and have problem with remote file upload. I followed
the instructions from this page
and getting error
undefined method `content_type' in

anybody handled this problem in rails 2 ?