如何使用Restkit + iOS向Rails进行POST请求

5

我的Rails应用有一个带有“名称”属性的Post模型。我正在尝试使用RestKit从iOS创建一个新的Post。以下是iOS代码。

NSDictionary* params = [NSDictionary dictionaryWithObject:@"amazingname" forKey:@"name"];
[[RKClient sharedClient] post:@"/posts" params:params delegate:self];

以下是在同一台机器上运行的服务器日志

    Started POST "/posts" for 127.0.0.1 at Tue May 10 15:39:14 -0700 2011
  Processing by PostsController#create as JSON
  Parameters: {"name"=>"amazingname"}
MONGODB blog_development['posts'].insert([{"_id"=>BSON::ObjectId('4dc9be92be2eec614a000002')}])
Completed 406 Not Acceptable in 4ms

正如您所看到的,服务器收到了请求,但是名称没有保存到数据库中。一个无名的帖子被创建了。我的iOS代码有什么问题?
2个回答

6
当我将forKey:@"name"改为forKey:@"post[name]"时,它就能正常工作。

1

或者你可以这样做

NSDictionary* attributes = [NSDictionary dictionaryWithObject:@"amazingname" forKey:@"name"];
NSMutableDictionary *params = 
[NSMutableDictionary dictionaryWithObject:attributes forKey:@"post"];

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接