2
votes

This is my first project with EmberJS and Ember-CLI. I run the following commands:

ember new site  //version: 0.1.2
ember generate resource categories   

I get the following answer and the tests:

  create app/models/category.js
  create app/routes/categories.js
  create app/templates/categories.hbs

ember generate resource category

I get the following answer and the tests:

  create app/routes/category.js
  create app/templates/category.hbs
  identical app/models/category.js

The router.js file has:

Router.map(function() {
  this.resource('categories', function() { });
  this.resource('category', { path: 'categories/:category_id' }, function() { });
});

When I visit dev:4200/categories/1 I get the following error:

 GET http://dev:4200/categories/1 404 (Not Found)
 Error while processing route: category.index

Can anyone tell me what I'm missing? All the files are generated by ember-cli in their default state.

EDIT: I have a models/category.js

export default DS.Model.extend({
  name: DS.attr()
});

And in the routes/category.js

export default Ember.Route.extend({
  model: function(params){
    console.log({category_id: params.category_id});
//    return this.store.find('category', params.category_id);
  }
});

The route dev:4200/categories/1 works and console.log shows the value Object {category_id: "1"} but with return this.store I get the error above.

2

2 Answers

3
votes

I solve it by creating a FixtureAdapter and adding some fixtures to the model.

ember generate adapter application

export default DS.FixtureAdapter.extend({    
});

models/category.js

var Category = DS.Model.extend({
  name: DS.attr('string'),
  img: DS.attr('string'),
  cnt: DS.attr('number')
});

Category.reopenClass({
    FIXTURES: [
        {
            id: 1,
            name: "Writing a blog in Ember",
            cnt: 8,
        },
        {
            id: 2,
            name: "Writing a blog",
            cnt: 5
        }
    ]
});

export default Category;

The error was misleading. Now it works just fine. Thanks.

1
votes

I think you are expecting the behavior of a route not a resource.

The following will generate two routes category and category.index.

this.resource('category', { path: '/categories/:category_id' }, function() { });

Navigating to http://dev:4200/categories/1 is going to route you first through CategoryRoute then CategoryIndexRoute

Try this:

Router.map(function() {
  this.resource('categories', function() { });
  this.route('category', { path: '/categories/:category_id' }, function() { });
});

Or more canonically this.

Router.map(function() {
  this.resource('categories', function() {
    this.route('show', {path: '/:category_id'});
  });
});