Ajaxy image uploading with attachment_fu - undefined method ‘content type’ error

While Ajax commenting worked great in my last post, it did not apply to ajax image uploading. This is apparently because there is a security restriction with javascript preventing access to the file system. I was getting the following error in my development server (using attachment_fu to process the images).

Processing ImagesController#create (for 127.0.0.1 at 2008-06-10 22:55:13) [POST]
  Session ID: BAh7CToMY3NyZl9pZCIlZGJhYTNjYTExZWE2MzdjMWQ1MGE2YjUwOWEzNjVh%0AMjA6DnJldHVybl90bzA6DHVzZXJfaWRpBiIKZmxhc2hJQzonQWN0aW9uQ29u%0AdHJvbGxlcjo6Rmxhc2g6OkZsYXNoSGFzaHsABjoKQHVzZWR7AA%3D%3D--a5f5fe97ec5e10d7a380e09ed1b0affc1f8030ce
  Parameters: {"commit"=>"Create", "authenticity_token"=>"779890648cc4b51c83eea32eb9f184534d171083", "action"=>"create", "controller"=>"admin/images", "image"=>{"uploaded_data"=>"1313883603_7dc8ef882f_o.jpg"}}
  User Columns (0.003101)   SHOW FIELDS FROM `users`
  User Load (0.001356)   SELECT * FROM `users` WHERE (`users`.`id` = 1) LIMIT 1
  Image Columns (0.002243)   SHOW FIELDS FROM `images`

NoMethodError (undefined method `content_type' for "1313883603_7dc8ef882f_o.jpg":String):
    /vendor/plugins/attachment_fu/lib/technoweenie/attachment_fu.rb:294:in `uploaded_data='

It should look like this:

Processing ImagesController#create (for 127.0.0.1 at 2008-06-10 23:02:07) [POST]
  Session ID: BAh7CToMY3NyZl9pZCIlZGJhYTNjYTExZWE2MzdjMWQ1MGE2YjUwOWEzNjVh%0AMjA6DnJldHVybl90bzA6DHVzZXJfaWRpBiIKZmxhc2hJQzonQWN0aW9uQ29u%0AdHJvbGxlcjo6Rmxhc2g6OkZsYXNoSGFzaHsABjoKQHVzZWR7AA%3D%3D--a5f5fe97ec5e10d7a380e09ed1b0affc1f8030ce
  Parameters: {"commit"=>"Create", "authenticity_token"=>"779890648cc4b51c83eea32eb9f184534d171083", "action"=>"create", "controller"=>"admin/images", "image"=>{"uploaded_data"=>#}}
  User Columns (0.003132)   SHOW FIELDS FROM `users`
  User Load (0.001419)   SELECT * FROM `users` WHERE (`users`.`id` = 1) LIMIT 1

User this solution:
It involves repsonds_to_parent and using iframes.

However, make sure to change the following…

<% form_for(:asset, :url =>formatted_assets_path(:format => 'js'), :html => { :multipart => true, :target => 'upload_frame'}) do |form| %>

to…

<% form_for(:asset, :url => assets_path + ".js", :html => { :multipart => true, :target => 'upload_frame'}) do |form| %>

That should do the trick.

Alternative links/solutions:
Another helpful link

The responds_to_parent plugin

Comments