dingbat / nsrails

iOS/Mac OS framework for Rails
http://nsrails.com
MIT License
517 stars 37 forks source link

There is no property to not send the root model name in requests #61

Closed noelrocha closed 10 years ago

noelrocha commented 10 years ago

Hi,

there is no way to avoid NSRails to insert the model name in POSTs.

How to avoid that?

dingbat commented 10 years ago

Hi there,

I assume you mean the JSON being sent as {"user"=>{"name"=>"x", "email"=>"y"}} instead of {"name"=>"x", "email"=>"y"}, for example.

The method that JSON-izes your object, remoteDictionaryRepresentationWrapped: on NSRRemoteObject, takes a boolean parameter of whether or not this should happen. The issue is that NSRRequest, what forms the POST requests you're probably making, calls this with YES.

So as I see it you could do two things:

  1. Craft NSRRequests manually whose request bodies don't wrap the JSON, but then you'd have to do all the object update followup (which is just one line really) manually as well:

    NSRRequest *r = [NSRRequest requestToCreateObject:obj];
    r.body = [obj remoteDictionaryRepresentationWrapped:NO];
    [r sendAsynchronous:^(id ret, NSError *err) {
      //check for error
      [obj setPropertiesUsingRemoteDictionary:ret];
    }];

    Of course if you're doing this often it can be refactored & the burden isn't so bad.

  2. Or, you could go the more hacky way and override remoteDictionaryRepresentationWrapped: in your model to always return NO:

    - (NSDictionary *) remoteDictionaryRepresentationWrapped:(BOOL)wrapped {
      return [super remoteDictionaryRepresentationWrapped:NO];
    }

    This is less advised, and also you'd have to make sure you never want this wrapping to happen.

The other obvious alternative I suppose is to support the wrapping server-side. If you're using Rails this is a given, but of course looks like you aren't.

dingbat commented 10 years ago

Closing, feel free to comment if you have any questions.