How to POST an object to rails using RestKit?

后端 未结 2 536
滥情空心
滥情空心 2020-12-30 14:52

I\'m trying to make the magic happen with RestKit and Rails. I\'m using Rails 3.1.1, and RestKit 0.9.3 (not the master branch). I am using Core Data in my iOS app, so I\'m

相关标签:
2条回答
  • 2020-12-30 15:14

    I think the inverseMapping selector should be enough to make a serialization mapping

    RKManagedObjectMapping *listMapping = [RKManagedObjectMapping mappingForClass:[List class]];
    listMapping.primaryKeyAttribute = @"listID";
    
    [listMapping mapAttributes:@"title", nil];
    [listMapping mapKeyPath:@"id" toAttribute:@"listID"];
    [manager.mappingProvider setMapping:listMapping forKeyPath:@"list"];
    //this should be enough
    [manager.mappingProvider setSerializationMapping:[listMapping inverseMapping] forClass:[List class]];
    

    Edit:

    according to our chat conversation, the warnings produced by RestKit were caused by the fact that the server response was lacking the root KeyPath (returned as naked array). To solve the problem we need to tell RestKit to use specific mapping to map the response:

    mapping = [[[RKObjectManager sharedManager] mappingProvider] mappingForKeyPath:@"list"];
    [[RKObjectManager sharedManager] postObject:newList mapResponseWith:mapping delegate:self];
    
    0 讨论(0)
  • 2020-12-30 15:24

    This is from the RestKit docs. There is a convenience method that sets both mapping and serialization mapping

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

    This will call setMapping:forKeyPath: for you, then generate a serialization mapping and set the root keyPath as well.

    0 讨论(0)
提交回复
热议问题