Wednesday, May 16, 2012

sending an image from iphone to rails. using RestKit

I am trying to send a sample Image to rails application and this is what i get for the parameters.



Started POST "/itemposts.json" for 127.0.0.1 at 2012-05-15 04:39:46 +0530
Processing by ItempostsController#create as JSON
Parameters: {"carryItem"=>"0", "description"=>"Hkhgcgc ", "makeOffer"=>"1", "price"=>"11336655", "title"=>"hkvlhkv", "assets_attributes"=>#<ActionDispatch::Http::UploadedFile:0x007fa5924327d8 @original_filename="
assets_attributes", @content_type="image/png", @headers="Content-Disposition: form-data; name=\"assets_attributes\"; filename=\"assets_attributes\"\r\nContent-Type: image/png\r\n", @tempfile=#<File:/var/folders/26/
yjdt74rx3cbg69hznd37fmyc0000gp/T/RackMultipart20120515-21256-630xb6>>}
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."remember_token" = 's4cUEfOHhnhID6pFAOlk2w' LIMIT 1
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."remember_token" = 's4cUEfOHhnhID6pFAOlk2w' LIMIT 1
Completed 500 Internal Server Error in 22ms

ArgumentError (Hash or Array expected, got ActionDispatch::Http::UploadedFile (#<ActionDispatch::Http::UploadedFile:0x007fa5924327d8 @original_filename="assets_attributes", @content_type="image/png", @headers="Cont
ent-Disposition: form-data; name=\"assets_attributes\"; filename=\"assets_attributes\"\r\nContent-Type: image/png\r\n", @tempfile=#<File:/var/folders/26/yjdt74rx3cbg69hznd37fmyc0000gp/T/RackMultipart20120515-21256-
630xb6>>)):
app/controllers/itemposts_controller.rb:15:in `create'


Rendered /Users/rajeshmedampudi/.rvm/gems/ruby-1.9.2-p318@global/gems/actionpack-3.2.2/lib/action_dispatch/middleware/templates/rescues/_trace.erb (3.2ms)
Rendered /Users/rajeshmedampudi/.rvm/gems/ruby-1.9.2-p318@global/gems/actionpack-3.2.2/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (1.1ms)
Rendered /Users/rajeshmedampudi/.rvm/gems/ruby-1.9.2-p318@global/gems/actionpack-3.2.2/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (12.5ms)


This is the response for the following code at the iPhone. using RestKit.



        NSDictionary *paramsDictionary=[NSDictionary dictionaryWithObjectsAndKeys: TitleField.text,@"title", PriceField.text,@"price",DescriptionField.text,@"description",[NSNumber numberWithBool:makeOfferButton.selected],@"makeOffer",[NSNumber numberWithBool:carryItemButton.selected],@"carryItem",nil];

RKParams *params=[RKParams paramsWithDictionary:paramsDictionary];
NSData *imageData=UIImagePNGRepresentation([UIImage imageNamed:@"2009-08-24 13.05.44.jpg"]);
[params setData:imageData MIMEType:@"image/png"forParam:@"assets_attributes"];
// [params setData:imageData MIMEType:@"image/png"forParam:@"assets"];
// [params setFile:@"bio.txt"forParam:@"attachment"];

[[RKClient sharedClient] post:@"/itemposts.json"params:params delegate:self];


The set up at the rails side strictly follows the tutorials from



http://www.emersonlackey.com/article/rails-paperclip-multiple-file-uploads
http://www.emersonlackey.com/article/paperclip-with-rails-3



and i have also tried out the example in http://brainbowapps.com/articles/2010/uploading-files-from-iphone-to-rails.html with no desired output.



What i need is basically to upload images and video files to s3 using paperclip gem...



The rails app is deployed in heroku.





No comments:

Post a Comment