Argus9
Argus9

Reputation: 1955

Customize request format in Ember

I am building a Rails 5 backend API which will receive requests from my Ember app. However I'm having some trouble getting Ember to format the request in a way my Rails server understands.

By default, Rails creates controllers to expect parameters in this format, assuming the model is a, say, Car:

"car": {
  "id": "1",
  "name": "Foo",
  "bar": "Bar",
  ...
}

However it looks like Ember is sending requests in this format:

"data": [
  {
    id: "1",
    type: "cars",
    attributes: {
      "name: "Foo",
      "bar": "Bar",
      ...
  }
]

What can I do to make Ember send request payloads in a way my Rails server will understand? Thank you.

Upvotes: 1

Views: 102

Answers (1)

Ember Freak
Ember Freak

Reputation: 12872

Your Rails is accepting REST adapter format, for that to work properly, your adapter should extend DS.RESTAdapter and serializer should extend DS.RESTSerializer. By default it will comes with JSONAPIAdapter and JSONAPISerializer.

If you are having control over the back end code, then consider writing json-api format response for that ember will work out of the box.

Reference:
https://emberjs.com/api/ember-data/2.14/classes/DS.RESTAdapter
https://emberjs.com/api/ember-data/2.14/classes/DS.RESTSerializer

https://emberjs.com/api/ember-data/2.14/classes/DS.JSONAPIAdapter https://emberjs.com/api/ember-data/2.14.9/classes/DS.JSONAPISerializer

Upvotes: 1

Related Questions