Tom Söderlund
Tom Söderlund

Reputation: 4747

How do I make AFIncrementalStore post data to Rails as objects?

I have an iOS + Rails 3.1 app, and I'm using AFIncrementalStore for the client-server communication. It's a sort of calendar app, and Activity is the main model.

When I create a new Activity using a Rails web form, the server receives this:

Started POST "/activities" for 127.0.0.1 at 2013-06-21 22:38:04 +0200
  Processing by ActivitiesController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"pruaVLfUijjNrYfh17yJmQgYLrnrA713OjgdayudZAg=", "activity"=>{"text"=>"Test från web", "starts_at_formatted"=>"23:00"}, "commit"=>"Create Activity"}

But when I post from my iOS app, it looks like this:

Started POST "/activities" for 127.0.0.1 at 2013-06-21 22:36:10 +0200
  Processing by ActivitiesController#create as JSON
  Parameters: {"activityID"=>"0", "auth_token"=>"xkT2eqqdoNp5y4vQy7xA", "ends_at"=>nil, "starts_at"=>"2013-06-21T22:36:10+0200", "text"=>"Inserted!", "updated_at"=>nil}
WARNING: Can't verify CSRF token authenticity

I.e. I'm missing the "activity"=> {...} bit that wraps the Activity data. How do I achieve this, do I need to do a massive overhaul of representationOfAttributes in my AFRESTClient <AFIncrementalStoreHTTPClient> subclass?

Upvotes: 1

Views: 132

Answers (1)

spinosa
spinosa

Reputation: 69

You need to implement -representationOfAttributes:ofManagedObject: in your AFRestClient subclass to wrap the dictionary returned by default.

If the default attribute mapping is already working, it's probably as simple as this:

- (NSDictionary *)representationOfAttributes:(NSDictionary *)attributes ofManagedObject:(NSManagedObject *)managedObject
{
    return @{[managedObject.entity.name lowercaseString]: [super representationOfAttributes:attributes ofManagedObject:managedObject]};
}

If you need to do some custom attribute mapping, just do that in place of my call to super.

Upvotes: 0

Related Questions