Nested model in a HTTP POST with application/json content type
I have a class structure like this:
class Parent < ActiveRecord::Base
has_one :child
accepts_nested_attributes_for :child
end
...
class Child < ActiveRecord::Base
belongs_to :parent
validates :text, :presence => true
end
In my mobile app (IOS + RestKit), I submit a HTTP POST
request to create a new Parent
object. This request has Content-Type application/json
The request looks like this on the wire:
{"parent":{"field1":"value1","child":{"text":"textvalue"},"duration":100}}
When 开发者_StackOverflowRails receives it, and tries to save it, I get the error:
ActiveRecord::AssociationTypeMismatch (Child(#2198796760) expected, got ActiveSupport::HashWithIndifferentAccess(#2158000780)):
Anyone know what's going on?
To expand one what Sameer said, you need to send child_attributes instead of child. This means you can't use the same mapping for pulling from the server and then pushing to it.
In RestKit you can specify a custom serialization that's different from the original object mapping. Here's an example that posts to a rails app, where Object has_many Images
//Map Images
RKManagedObjectMapping* imageMapping = [RKManagedObjectMapping mappingForEntityWithName:@"Image"];
imageMapping.setNilForMissingRelationships = YES;
imageMapping.primaryKeyAttribute = @"imageId";
[imageMapping mapKeyPathsToAttributes:@"id", @"imageId", @"is_thumbnail", @"isThumbnail", @"image_caption", @"imageCaption", @"image_data", @"imageData", nil];
//Serialize Images
RKManagedObjectMapping* imageSerialization = (RKManagedObjectMapping*)[imageMapping inverseMapping];
imageSerialization.rootKeyPath = @"image";
[imageSerialization removeMappingForKeyPath:@"imageId"];
//Map Objects
RKManagedObjectMapping* objectMapping = [RKManagedObjectMapping mappingForEntityWithName:@"Object"];
objectMapping.setNilForMissingRelationships = YES;
objectMapping.primaryKeyAttribute = @"objectId";
[objectMapping mapKeyPath:@"id" toAttribute:@"objectId"];
[objectMapping mapRelationship:@"images" withMapping:imageMapping];
//Serialize Objects
RKManagedObjectMapping* objectSerialization = (RKManagedObjectMapping*)[objectMapping inverseMapping];
objectSerialization.rootKeyPath = @"object";
[objectSerialization removeMappingForKeyPath:@"images"];
[objectSerialization removeMappingForKeyPath:@"objectId"];
[objectSerialization mapKeyPath:@"images" toRelationship:@"images_attributes" withMapping:imageSerialization];
[objectManager.mappingProvider setMapping:objectMapping forKeyPath:@"object"];
[objectManager.mappingProvider setSerializationMapping:objectSerialization forClass:[Object class]];
Note that it's also important to remove the ID attributes when posting - that gave me no end of trouble, since it doesn't seem like it should matter in a post, but it throws rails off.
For what it's worth, I also had issues parsing the nested objects with rails, and had to change my controller to look like this:
def create
images = params[:object].delete("images_attributes");
@object = Object.new(params[:object])
result = @object.save
if images
images.each do |image|
image.delete(:id)
@object.images.create(image)
end
end
respond_to do |format|
if result
format.html { redirect_to(@object, :notice => 'Object was successfully created.') }
format.json { render :json => @object, :status => :created, :location => @object }
else
format.html { render :action => "new" }
format.json { render :json => @object.errors, :status => :unprocessable_entity }
end
end
end
That could (and probably should) be moved into a before_create filter on the Object model.
I hope that helps in some way.
Instead of "child" in your params hash, use "child_attributes".
精彩评论