Uploading to Ruby on Rails from iPhone using ASIHTTPRequest
I've really hit a wall and am need of some help! Thankyou for reading this far!
I'm in the middle of writing an app that talks to my ROR web-server for database requests and works great thanks to ActiveResource. But I now need to also upload files to the server, and I plan to开发者_开发问答 use ASIHTTPRequest which looks great, my problem though is I'm just not sure how to hand the POST request on the ROR side... I'm using paperclip but have really hit a brick wall.
On the ASIHTTP side I'm simply writing:
[request setData:data withFileName:@"photo.jpg" andContentType:@"image/jpeg" forKey:@"asset[image]"];
and on the ruby side I'm doing...
class Asset < ActiveRecord::Base
validates_attachment_presence :image
has_attached_file :image
end
class AssetsController < ApplicationController
protect_from_forgery :only => [:update, :destroy]
.....
But it always fails, I'm pretty sure it's got something to do with the POST form dataset, but I am completely stuck.
I'm getting the error:
Parameters: {"assets"=>{"images"=>#<File:/var/folders/gM/gM15qjM2G3W0iVNaT1evD++++TI/-Tmp-/RackMultipart20091112-2285-2i0qq5-0>}} NoMethodError (You have a nil object when you didn't expect it! You might have expected an instance of ActiveRecord::Base. The error occurred while evaluating nil.[]): app/models/asset.rb:2 app/controllers/assets_controller.rb:46:in
`create'
Any help would be very very gratefully received.
Chris
Thanks!
The first thing I'd check with doing the upload is setting the parameter name to what file_column (or whatever, personally I'd use Paperclip) is expecting.
If you have something like:
class Entry < ActiveRecord::Base
file_column :image
end
You'll need to make sure the parameter (form field name) corresponds to what is expected. For the above example this would be:
name="entry[image]"
Also, make sure you're doing a multipart form post, not just the standard.
精彩评论