gpt4 book ai didi

objective-c - Rails RestKit POST 请求 json 的根类丢失

转载 作者:太空狗 更新时间:2023-10-30 03:54:16 25 4
gpt4 key购买 nike

我之前遇到过这个问题并已修复,但我知道修复的方式肯定不正确。有人可以为我指明正确的方向以进行正确的修复吗?

基本上,我通过 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

一位友善的先生指出我的 create 函数中的类只接受一个我忽略的 :person 类。

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

我通过这样做解决了这个问题

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

这是我的代码:

@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;
@end

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"];

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];
}

最佳答案

Person为例:

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] 技巧同时执行 setMappingsetSerializationMapping

关于objective-c - Rails RestKit POST 请求 json 的根类丢失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8856180/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com