objective-c – Rails RestKit POST请求缺少json的根类

objective-c – Rails RestKit POST请求缺少json的根类,第1张

概述嗨伙计们,我有这个我修复过的问题,但我知道修复是没有做正确的方法.有人能指出我正确的方向进行正确的修复吗? 所以基本上我通过RESTkit postObject使用iOS sim创建一个对象,我收到了这条消息 Processing PeopleController#create (for 127.0.0.1 at 2012-01-13 03:55:46) [POST] Parameters: 嗨伙计们,我有这个我修复过的问题,但我知道修复是没有做正确的方法.有人能指出我正确的方向进行正确的修复吗?

所以基本上我通过RESTkit postObject使用iOS sim创建一个对象,我收到了这条消息

Processing PeopleController#create (for 127.0.0.1 at 2012-01-13 03:55:46) [POST]  Parameters: {"name"=>"data"}Person Create (0.4ms)   INSERT INTO "people" ("created_at","updated_at","name")VALUES('2012-01-13 11:55:46','2012-01-13 11:55:46',NulL)Completed in 27ms (VIEw: 1,DB: 0) | 200 OK

一位优秀的先生们指出,我的创建函数中的类只接受了一个我忽略的人类.

def create@person = Person.new(params[:person]),thus looking for {"person" => {"name"=>"data"}}

我这样做了

@person = Person.new(姓名:=> params [:name]),因为我只发送{“name”=>“data”}
现在它创建好了,我可以看到我的ios sim上的条目,但我知道这不是正确的方式应该是{“person”=> {“name”=>“data”}}发送到原始函数.另外我的错误找不到keyPath的对象映射:“有什么想法吗?提前thnx!
这是我的xcode

@interface Data: NSObject{// imaginary over arching class   Person * person;   NSArray *dog;@property (nonatomic,retain) Person * person;@property (nonatomic,retain) NSArray * dog;@interface Data : NSObject {Person *person;NSArray *dogs;}@property (nonatomic,retain) Person *person;@property (nonatomic,retain) NSArray *dogs;@end@interface Person : NSObject {Nsstring *name;NSNumber *personID;NSDate   *updatedAt;NSDate   *createdAt;}@property (nonatomic,retain) NSDate * createdAt;@property (nonatomic,retain) NSDate * updatedAt;@property (nonatomic,retain) NSNumber  *personID;@property (nonatomic,retain) Nsstring *name;@endRKObjectMapPing* userMapPing = [RKObjectMapPing mapPingForClass:[Person class]];[userMapPing mapKeyPath:@"created_at" toAttribute:@"createdAt"];[userMapPing mapKeyPath:@"updated_at" toAttribute:@"updatedAt"];[userMapPing mapKeyPath:@"name" toAttribute:@"name"]; [userMapPing mapKeyPath:@"ID" toAttribute:@"personID"];RKObjectMapPing* dogMapPing = [RKObjectMapPing mapPingForClass:[Dog class]];[dogMapPing mapKeyPath:@"created_at" toAttribute:@"createdAt"]; [dogMapPing mapKeyPath:@"person_ID" toAttribute:@"spersonID"]; [dogMapPing mapKeyPath:@"name" toAttribute:@"name"]; [dogMapPing mapKeyPath:@"updated_at" toAttribute:@"updatedAt"]; [dogMapPing mapKeyPath:@"ID" toAttribute:@"dogID"]; RKObjectMapPing *dataMapPing = [RKObjectMapPing mapPingForClass:[Data class]]; [dataMapPing mapKeyPath:@"dog" toAttribute:@"dogs"]; [dataMapPing mapKeyPath:@"person" toRelationship:@"person" withMapPing:userMapPing]; [[RKObjectManager sharedManager].mapPingProvIDer addobjectMapPing:dataMapPing]; [[RKObjectManager sharedManager] loadobjectsAtResourcePath:@"/people"       objectMapPing:dataMapPing delegate:self]; RKObjectRouter * router = [RKObjectManager sharedManager].router;[router routeClass: [Person class] toResourcePath:@"/people/:personID"];[router routeClass: [Person class] toResourcePath:@"/people"          forMethod:RKRequestMethodPOST];RKObjectMapPing *personSerializationMapPing = [RKObjectMapPing mapPingForClass:[NSMutableDictionary class]];[personSerializationMapPing attribute:@"name",nil];[RKObjectManager sharedManager].mapPingProvIDer setSerializationMapPing:personalSerializationMapPing forClass: [Person class]];Person *dave = [[Person alloc]init];dave.name = @"Dave";[[RKObjectManager sharedManager] postObject:dave delegate:self];}
解决方法 以人为例:

RKObjectMapPing* userMapPing = [RKObjectMapPing mapPingForClass:[Person class]];[userMapPing mapKeyPath:@"created_at" toAttribute:@"createdAt"];[userMapPing mapKeyPath:@"updated_at" toAttribute:@"updatedAt"];[userMapPing mapKeyPath:@"name" toAttribute:@"name"]; [userMapPing mapKeyPath:@"ID" toAttribute:@"personID"];

要使用根路径将RestKit配置为POST,请替换:

[[[RKObjectManager sharedManager] mapPingProvIDer]          setSerializationMapPing:personalSerializationMapPing                         forClass:[Person class]];

有:

[[[RKObjectManager sharedManager] mapPingProvIDer]                 registerMapPing:objectMapPing                  withRootKeyPath:@"person"];

现在Rails将收到{“person”=> {“name”=>“data”}}而不是{“name”=>“data”}.

作为参考,这里是registerMapPing的魔力:

- (voID)registerObjectMapPing:(RKObjectMapPing *)objectMapPing withRootKeyPath:(Nsstring *)keyPath {    // Todo: Should generate logs    objectMapPing.rootKeyPath = keyPath;    [self setMapPing:objectMapPing forKeyPath:keyPath];    RKObjectMapPing* inverseMapPing = [objectMapPing inverseMapPing];    inverseMapPing.rootKeyPath = keyPath;    [self setSerializationMapPing:inverseMapPing forClass:objectMapPing.objectClass];}

它使用聪明的[objectMapPing inverseMapPing]技巧同时执行setMapPing和setSerializationMapPing.

总结

以上是内存溢出为你收集整理的objective-c – Rails RestKit POST请求缺少json的根类全部内容,希望文章能够帮你解决objective-c – Rails RestKit POST请求缺少json的根类所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1045602.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-24
下一篇 2022-05-24

发表评论

登录后才能评论

评论列表(0条)

保存