simple AJAX file upload for Rails

231 views
Skip to first unread message

RainChen

unread,
Jun 29, 2007, 3:16:31 PM6/29/07
to Ruby on Rails: Talk
Normally, to upload file,we will write some codes in the view page
like:

<% form_for(:photo, :url => {:action => :create}, :html =>
{ :multipart => true}) do |form| -%>
<p>
<label for="">Upload A Image:</label>
<%= form.file_field :uploaded_data %>
<%= submit_tag 'Upload' %>
</p>
<% end -%>

Now,I coded some line of js, append to public/javascript/
application.js
now we just need to change little things as:
change: form_for => remote_form_for
add: :ajaxupload => true

Then we got:

<% remote_form_for(:photo, :url => {:action => :create}, :html =>
{ :multipart => true, :ajaxupload => true}) do |form| -%>
<p>
<label for="">Upload A Image:</label>
<%= form.file_field :uploaded_data %>
<%= submit_tag 'Upload' %>
</p>
<% end -%>

Now, file upload will post via a iframe without refresh the page.

But how about the rjs?
We won't forget it.
Add some codes in the controller as bellow:

# iframe request
if params[:HTTP_X_REQUESTED_WITH] && request.post?
render :template => 'photos/create.rjs', :layout =>
false, :content_type => 'text/html'
else
# normal request
respond_to do |format|
format.html {redirect_to :action => 'new', :id => @photo if
request.post?}
format.js
end
end

Now we can render different views by the request type automatically.

The js code could down from:
http://www.rainchen.com/rails/photo_upload/application.js

and a complete photo upload rails demo project:
http://www.rainchen.com/rails/photo_upload/photo_upload.rar
use attachment_fu plugin (require RMagick) and sqlite3 db engline

Tested with IE6,FireFox2.0 on windows XPsp2.
With Safari, upload OK,but evalScripts false.


some helpful links:

attachment_fu tutorial:
http://clarkware.com/cgi/blosxom/2007/02/24#FileUploadFu

RMagick for windows:
http://rubyforge.org/frs/download.php/15132/RMagick-1.14.1_IM-6.3.0-7-Q8.zip

sqlite3 lib for windows:
http://www.sqlite.org/sqlite-3_4_0.zip
http://www.sqlite.org/sqlitedll-3_4_0.zip


last line:
I'm new to Rails, sorry for the rough codes. Any improvement is
wellcome.

Rain Chen

unread,
Jul 4, 2007, 10:56:33 AM7/4/07
to rubyonra...@googlegroups.com
Update:
Test passed with Safari3

--
Posted via http://www.ruby-forum.com/.

Chamnap

unread,
Jul 15, 2007, 10:32:25 PM7/15/07
to Ruby on Rails: Talk
Hi,

why need to add :ajaxupload => true?

Rain Chen

unread,
Jul 16, 2007, 1:23:50 AM7/16/07
to rubyonra...@googlegroups.com
Chamnap wrote:
> Hi,
>
> why need to add :ajaxupload => true?

that's the magic tag to tell the program to process using AJAX way.

Rain Chen

unread,
Jul 16, 2007, 12:46:24 PM7/16/07
to rubyonra...@googlegroups.com
Chamnap wrote:
> Hi,
>
> why need to add :ajaxupload => true?

aha, I may got what your doubt.
OK, I removed the :ajaxupload option.
Now you just only need to change the "form_for" to "remote_form_for",
then the file can upload behind the screen.

jc-denton

unread,
Jul 26, 2007, 8:47:39 AM7/26/07
to Ruby on Rails: Talk
>> why need to add :ajaxupload => true?
> that's the magic tag to tell the program to process using AJAX way.

That's exactly that kind of answer that I HATE, because it's as good
as not answering it. Please provide a real answer. I could not find
this option in the API, does it have something to do with the
javascript that you include, does it alter the stuff in onclick=""?

Reply all
Reply to author
Forward
0 new messages