ember.jsember-dataember-model

Ember Data incompatible with Rails JSON response: Encountered a resource with an undefined type


I'm trying to set up Ember Data to talk to a Rails backend. Ember succeeds at calling the endpoint, gets both a 200 and some response data. The problem is that Ember is unable to match the response data to one of my Ember models I've made.

My model code:

export default DS.Model.extend({
  user_id: DS.belongsTo('user'),
  transcription: DS.attr('string'),
  transcription_time: DS.attr('date'),
  transcription_date: DS.attr('date'),
  recording_url: DS.attr('string'),
  state: DS.attr('string')
});

Below is an image of how the backend returns data along with the error from Ember:

enter image description here

Error: Encountered a resource with an undefined type.

I don't need to include created_at or updated_at in my Ember models do I? Also, how does Ember know to connect user_id with my User Ember model? Is that what's causing the error? I specified user_id to belongTo('user') though. Do I need to explicitly include type field in my JSON response? This doesn't seem right though.

I've read a couple of Ember guides, including their guide on models and their guide on serializers. I don't need to write a custom JSONAPISerializer, do I? Most of the guides I read did not say to do this. Any help and tips would be greatly appreciated!


Solution

  • Your JSON response should include a "type" field, according to the JSONAPI spec. Here's what the JSON from my rails app looks like:

    {"data":{"attributes":{"title":"asdf","body":"adsf"},"type":"posts"}}
    

    Are you using Active Model Serializers on the rails side, and did you configure it to use the JSONAPI renderer?

    All you have to do is set ActiveModelSerializers.config.adapter in an initializer:

    # config/initializers/jsonapi.rb
    ActiveModelSerializers.config.adapter = :json_api
    

    It should include the type field automatically.

    If you still have trouble, I think if you share some of your rails code I can help.