Rails RestKit POST 请求 json 的根类丢失

发布于 2024-12-27 07:00:18 字数 3443 浏览 0 评论 0原文

我之前遇到过这个问题,但我已经修复了,但我知道修复方法肯定没有正确完成。有人可以指出我正确的方向以进行正确的修复吗?

基本上,我通过 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 的对象映射:“。有什么想法吗?

这是我的 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;
@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];
}

I had this previous problem which I fixed, but I know the fix is def not done the right way. Can some one point me in the right direction for the right fix?

Basically I create a object with iOS sim via RESTkit postObject, and I got this message:

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 

A nice gentlemen pointed out that my class inside my create function is only accepting a :person class which I overlooked.

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

I fixed this by doing

@person =Person.new(name:=>params[:name]) since i am only sending {"name"=>"data"}
now it creates ok, and I can see the entry on my ios sim. But I know this is not the right way. The right way should be {"person" => {"name"=>"data"}} sent in to the original function. Plus I get an error could not find an object mapping for keyPath:". Any thoughts?

Here is my 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;
@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];
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

春风十里 2025-01-03 07:00:18

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 将收到 {"人”=> {"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];
}

它通过巧妙的 实现 setMappingsetSerializationMapping [objectMapping inverseMapping] 技巧。

Take Person as an example:

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

To config RestKit to POST with a root path, replace:

[[[RKObjectManager sharedManager] mappingProvider] 
         setSerializationMapping:personalSerializationMapping 
                        forClass:[Person class]];

with:

[[[RKObjectManager sharedManager] mappingProvider]
                 registerMapping:objectMapping 
                 withRootKeyPath:@"person"];

Now Rails will receive {"person" => {"name"=>"data"}} instead of {"name"=>"data"}.

For reference, here is the magic of 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];
}

it does both setMapping and setSerializationMapping with the clever [objectMapping inverseMapping] trick.

铃予 2025-01-03 07:00:18

是的,终于想通了。映射是错误的。它应该是 userMappingkeyPath person,而不是 addObjectMapping: dataMapping,即 userMapping。顺便说一句,thnx @favo 编辑我的鸡抓痕 =] 现在它可以 GETPOST 很好!

Yea finally figured it out. The mapping was wrong. Instead of addObjectMapping: dataMapping, it should have been userMapping with keyPath person which is userMapping. btw thnx @favo for editing my chicken scratch =] now it can GET and POST fine!

旧话新听 2025-01-03 07:00:18

为了完成,关于“无法找到关键路径''的映射”的错误似乎是Rails创建操作的问题。

这个问题有一个很好的答案:如何使用 RESTKit 将对象发布到 Rails

For completionist sake, the error about "could not find mapping for key path ''" seems to be a thing with Rails create action.

There's a good answer for this problem in this SO question: how to post an object to Rails using RESTKit

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文