0

I keep getting a "No model was found for '0'" error when trying to connect an Ember.js app to a Rails 3.2 API. My setup is below. Any help will be greatly appreciated.

Items Controller (Rails)

def index

  @items = Item.all

  respond_to do |format|
    format.html
    format.json { render json: @items, root: true }
  end

end

App.js (Ember.js)

App = Ember.Application.create();

App.Router.map(function() {
  this.resource('items', function() {
    this.route('backlog');
    this.route('board');
  });
});


App.ItemsRoute = Ember.Route.extend({
  model: function() {
    return this.store.find('item');
  }
});

Server response when the Ember app makes request to /items

[
  {
    "item": {"id":1,"item_type":"Item","name":"Test item"}
  },
  { 
    "item": {"id":2,"item_type":"Item","name":"Test item 2"}
  }
]

1 Answer 1

2

Assuming that you're using Ember-Data, your JSON format is incorrect. It's fairly poorly documented, so don't feel bad. The RESTAdapter has a brief example, but the JSON API goes into more details. In your case, I think you want your response to look like this:

{
    "items": [
        { "id": 1, ... },
        { "id": 2, ... }
    ]
}
Sign up to request clarification or add additional context in comments.

2 Comments

I am using Ember-Data and have seen that this format is expected. How do I configure Rails and/or my controller to respond with this format?
I got it working using the active_model_serializers gem. After adding the gem and creating a serializer for my item model (with rails g serializer item) it responds with the json format you describe and it works!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.