2011-06-28 119 views
8

在Rails 3.0.8中,json包含一个包含模型名称的根元素。例如我的位置模型。RestKit与Rails 3.1的映射

[ 
{ 
location: { 
city: San Diego 
name: Mission Valley YMCA Krause Family Skatepark 
pads_required: 0 
country: United States 

而映射提供程序直接查找位置对象。

RKObjectMapping* locationMapping = [RKObjectMapping mappingForClass:[RKLocation class]]; 
[locationMapping mapKeyPath:@"id" toAttribute:@"locationId"]; 
... 
[objectManager.mappingProvider setMapping:locationMapping forKeyPath:@"location"]; 

现在,当你升级到3.1.0轨道根节点“位置”默认情况下,现在已经删除,我不知道如何配置映射提供无过?我尝试了零,并寻找替代方法,但没有成功。

你知道如何映射这个吗?请帮忙!

[ 
{ 
    city: San Diego 
    name: Mission Valley YMCA Krause Family Skatepark 
    pads_required: 0 
    country: United States 

回答

7

从RestKit的一面,我不知道,但是从this topic它看起来像你可以通过执行获得JSON回到了RestKit预计:

class Location < ActiveRecord::Base 
    self.include_root_in_json = true 
end 

编辑: 为了完整起见,这里是你如何与RestKit做到这一点:

RKObjectMapping* locationMapping = [RKObjectMapping mappingForClass:[RKLocation class]]; 
[locationMapping mapKeyPath:@"id" toAttribute:@"locationId"]; 
... 
[objectManager.mappingProvider addObjectMapping:locationMapping]; 

再后来调用映射器:

RKObjectMapping* locationMapping = [[RKObjectManager sharedManager].mappingProvider objectMappingForClass:[RKLocation class]]; 
[[RKObjectManager sharedManager] loadObjectsAtResourcePath:@"/locations" objectMapping:locationMapping delegate:self]; 

然后你会处理RKObjectLoader委托方法中的对象。

4

在RestKit,你可以注册一个映射包含一个根模型名称是这样的:

[objectManager.mappingProvider registerMapping:locationMapping withRootKeyPath:@"location"]; 
0

简短而亲切的回答:forKeyPath:@""会工作。