hugomarisco
hugomarisco

Reputation: 336

How to configure Ember Data to work with Rails backend?

First of all, I know this should be pretty straightforward and that many documentation seem to answer my question. The thing is, I've tried everything and I can't make it work.

So I'm trying to start my first Ember Application.

I've created a new Rails App, added ember-rails Gem, ran ember:bootstrap and ember:install --head and this is how my setup looks like now:

DEBUG: ------------------------------- ember.js?body=1:3883
DEBUG: Ember      : 1.7.0-beta.1+canary.271940eb ember.js?body=1:3883
DEBUG: Ember Data : 1.0.0-beta.7+canary.20adb1d5 ember.js?body=1:3883
DEBUG: Handlebars : 1.3.0 ember.js?body=1:3883
DEBUG: jQuery     : 1.11.0 ember.js?body=1:3883
DEBUG: ------------------------------- 

Now I'm trying to simply grab some records from my rails API and displaying them.

I've configured my backend with an activeModel serializer and I've checked it's responding with correct resultset.

My store.js.coffee i (where I'm pretty sure the problem is) is the following:

App.ApplicationAdapter = DS.ActiveModelAdapter.extend()

And I'm always getting this error:

Error while loading route: TypeError: Object function () {
        var Class = makeCtor(), proto;
        Class.ClassMixin = Mixin.create(this.ClassMixin);
        Class.PrototypeMixin = Mixin.create(this.PrototypeMixin);

        Class.ClassMixin.ownerConstructor = Class;
        Cl...<omitted>... } has no method 'create'
    at instantiate (http://app.dev/assets/ember.js?body=1:46251:26)
    at lookup (http://app.dev/assets/ember.js?body=1:46117:19)
    at Object.Container.lookup (http://app.dev/assets/ember.js?body=1:45792:16)
    at EmberObject.extend.controllerFor (http://app.dev/assets/ember.js?body=1:38264:32)
    at EmberObject.extend.setup (http://app.dev/assets/ember.js?body=1:37790:31)
    at handlerEnteredOrUpdated (http://app.dev/assets/ember.js?body=1:40938:36)
    at http://app.dev/assets/ember.js?body=1:40907:18
    at forEach (http://app.dev/assets/ember.js?body=1:41956:54)
    at setupContexts (http://app.dev/assets/ember.js?body=1:40906:9)
    at finalizeTransition (http://app.dev/assets/ember.js?body=1:41076:9) 

Any thoughts?

Thank you

Upvotes: 3

Views: 115

Answers (1)

Avery
Avery

Reputation: 11

I recently ran into this exact same problem when connecting my Ember app to a rails backend. The reason I received this error was because the JSON data that I was sending to ember was not what Ember expected. Make sure that the dataset you are getting can translate into the models you defined in ember.

For example: If the data coming in looks like this:

{
  contacts: [
    {name:"contact1"....
    }, ...
  ],
  buildings: [
    {...
    }, ...
  ]
}

Then ember will expect there to be a model for contacts and a model for buildings. Additionally, make sure that if you define a model without any attributes in ember it is closed properly. I ran into that as well.

For example:

App.Contact = DS.Model.extend

needs to be

App.Contact = DS.Model.extend()

if the ember model has no attributes.

Upvotes: 1

Related Questions