title: 406 Not Acceptable - xhr image upload to rails
body: <p>I am trying to upload an image from iPhone to my working rails application that uses Carrierwave for processing. Do I need to account for the CSRF authenticity token that rails requires?</p>
  
  <p>I have the following in my Titanium app.js file, taken pretty much from their Snapost example code:</p>
  
  <pre><code>xhr.open('POST','http://myapp.com/foos/1/bars');
  xhr.send({media:originalImage});
  </code></pre>
  
  <p>My rails action is very simple:</p>
  
  <pre><code>def create
    @bar = @foo.bars.build(params[:bar])
    @bar.image = params[:file]    
    respond_to do |format|
      if @bar.save
        format.html { redirect_to(@foo, :notice =&gt; 'Bar was successfully created.') }
        format.json  { render :json =&gt; @bar, :status =&gt; :created }
        fotmat.xml { render :xml =&gt; @bar, :status =&gt; :created }
      else
        format.html { render :action =&gt; "new" }
        format.json { render :json =&gt; @bar.errors, :status =&gt; :unprocessable_entity }
        format.xml { render :xml =&gt; @bar.errors, :status =&gt; :unprocessable_entity }
      end
    end
  end
  </code></pre>
  
  <p>From my device, I get the 'xhr.onerror' alert describing a timeout when I attempt an upload. My server log is as follows:</p>
  
  <pre>
  Started POST &quot;/foos/1/bars&quot; for ###.###.#.### at 2011-05-01 17:01:33 -0500 
  
  Processing by BarsController#create as MULTIPART_FORM 
  
  Parameters: {&quot;media&quot;=#&lt;ActionDispatch::Http::UploadedFile:0xabdd968 @original_filename=&quot;285050.jpg&quot;, @content_type=&quot;image/jpeg&quot;, @headers=&quot;Content-Disposition: form-data; name=\&quot;media\&quot;; filename=\&quot;2
  85050.jpg\&quot;\r\nContent-Type: image/jpeg\r\n&quot;, @tempfile=#&lt;File:/tmp/RackMultipart20110501-32635-olgooh&gt;&gt;, &quot;foo_id&quot;=&gt;&quot;1&quot;} 
  
  ^[[1m^[[36mSQL (1.9ms)^[[0m ^[[1mSHOW TABLES^[[0m 
  
  ^[[1m^[[35mFoo Load (0.1ms)^[[0m SELECT foos.* FROM foos WHERE foos.id = 1 LIMIT 1 
  
  ^[[1m^[[36mSQL (2.6ms)^[[0m ^[[1mBEGIN^[[0m 
  
  ^[[1m^[[35mSQL (5.9ms)^[[0m ROLLBACK 
  
  Completed 406 Not Acceptable in 468ms
  
  </pre>