如何从ASIHTTPRequest向插入到DB中的Rails发出POST请求

时间:2011-05-05 09:19:40

标签: iphone ruby-on-rails-3

iOS的新手。我有一个rails博客应用程序和我用scaffold创建的Post模型。它有一个名称:字符串和图像。我正在使用ASIHTTPRequest创建一个新帖子。它连接到服务器很好,但参数“name”和“image”没有正确传递到数据库中。这是iphone代码:

NSURL *url=[[NSURL alloc] initWithString:@"http://localhost:3000/posts"];
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
    [request setPostValue:@"Ben" forKey:@"name"];
    [request setFile:@"/Users/Seb/Desktop/beach.jpeg" forKey:@"image"]; 
    [request startSynchronous];    

以下是rails服务器日志显示的内容:

Started POST "/posts" for 127.0.0.1 at Wed May 04 15:41:43 -0700 2011
  Processing by PostsController#create as HTML
  Parameters: {"name"=>"Ben", "image"=>#<ActionDispatch::Http::UploadedFile:0x103eb33a0 @content_type="image/jpeg", @original_filename="beach.jpeg", @headers="Content-Disposition: form-data; name=\"image\"; filename=\"beach.jpeg\"\r\nContent-Type: image/jpeg\r\n", @tempfile=#<File:/var/folders/PI/PI+kcHrCHfuDh-K+ppxAxE+++TI/-Tmp-/RackMultipart20110504-11613-zvv5im-0>>}
MONGODB blog_development['posts'].insert([{"_id"=>BSON::ObjectId('4dc1d627be2eec2d5d00000f')}])
Redirected to http://localhost:3000/posts/4dc1d627be2eec2d5d00000f
Completed 302 Found in 5ms

如您所见,名称和图像不会插入到数据库中。当我从浏览器发布帖子时,服务器日志显示:

Started POST "/posts" for 127.0.0.1 at Mon May 02 00:54:52 -0700 2011
  Processing by PostsController#create as HTML
  Parameters: {"commit"=>"Create Post", "post"=>{"name"=>"tiny face", "image"=>#<ActionDispatch::Http::UploadedFile:0x1042b2bb8 @content_type="image/png", @original_filename="Screen shot 2011-05-01 at 11.23.23 PM.png", @headers="Content-Disposition: form-data; name=\"post[image]\"; filename=\"Screen shot 2011-05-01 at 11.23.23 PM.png\"\r\nContent-Type: image/png\r\n", @tempfile=#<File:/var/folders/PI/PI+kcHrCHfuDh-K+ppxAxE+++TI/-Tmp-/RackMultipart20110502-11613-u7czau-0>>}, "authenticity_token"=>"i+4h+XOOuJyTnF+quru8yrBuM1ixlDXC5udYaB/0jP4=", "utf8"=>"✓"}
MONGODB blog_development['posts'].insert([{"name"=>"tiny face", "image_filename"=>"screen_shot_2011-05-01_at_11.23.23_pm.png", "_id"=>BSON::ObjectId('4dbe634dbe2eec2d5d000002')}])
Redirected to http://localhost:3000/posts/4dbe634dbe2eec2d5d000002
Completed 302 Found in 518ms

知道如何构建我的POST以使其保存到db?

1 个答案:

答案 0 :(得分:1)

您将要发布的表的名称放在键名前的括号中。发布到rails应用程序时遇到了同样的问题。

[request setPostValue:@"Ben" forKey:@"table [name]"];
[request setFile:@"/Users/Seb/Desktop/beach.jpeg" forKey:@"table [image]"];