在 Rails 3.0.8 中,json 包含一个带有模型名称的根元素。例如我的位置模型。
[
{
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"];
现在,当您升级到 rails 3.1.0 时,默认情况下会删除根节点“位置”,我不确定如何在没有它的情况下配置映射提供程序?我尝试了 nil 并寻找替代方法,但没有成功。
你知道如何映射这个吗?请帮忙!
[
{
city: San Diego
name: Mission Valley YMCA Krause Family Skatepark
pads_required: 0
country: United States
最佳答案
从 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 委托(delegate)方法中的对象。
关于ruby-on-rails - 与 Rails 3.1 的 RestKit 映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6508380/