Posting to a Shallow Nested Route


#1

I’m using shallow nested routes (Rails) for some of my models. I’m able to access children records by including a links hash in the JSON returned object like so:

{
  "companies": [
  {
    "id": 1,
    "name": "Acme, Inc.",
    "links": {
      "locations": "/companies/1/locations"
    },
    "location_ids": [
      1
    ]
  }]
}

This works fine for GETs, but how would I do a POST against /companies/1/locations. Ember-Data tries to use /locations, which doesn’t exist. How do I get ember-data to use the links hash when creating a record?

BTW, my URLS look like so:

/companies/1/locations: GET

/companies/1/locations: POST

Then /locations/:id for PUT, GET (show), and DELETE


#2

Apparently this can’t be accomplished with ember-data “as is”. I believe you need to extend the adapter to build the URL the way you want it.

Here it’s explained a nice trick to do it extending the generic RESTAdapter: http://stackoverflow.com/questions/21530833/sending-rest-requests-to-a-nested-api-endpoint-url-using-ember-data/22806798#22806798

I did this only extending the createRecord for the POST route, since the updateRecord (PUT) and deleteRecord (DELETE) seem to work well if you use shallow nested routes. It worked for me!