user3379926
user3379926

Reputation: 3945

Not understanding fixtures in ember

I am trying to, for better lack of a term stub out my application's user model using fixtures. I have been following the docs on fixtures, as well as associated links for model development. The issue is that I keep running into cannot GET /user/Administrator I have used yeoman to generate a ember project, but lets walk through what I have:

Set up the app

var EW = window.EW = Ember.Application.create();

Setup the fixture (store.js)

EW.ApplicationAdapter = DS.FixtureAdapter;

/* I need a fixture. */
EW.User.FIXTURES = [
    {
       "user":{
          "id":1,
          "first_name":"Adam",
          "email":"[email protected]",
          "last_name":"Balan",
          "user_name": "Administrator",
          "role_names":[
             "Administrator"
          ],
          "blogs":[
             {
                "id":2,
                "title":"Blog Title",
                "posts":[
                   {
                      "id":1,
                      "title":"Something",
                      "content":"Some Content",
                      "tag_names":[
                         "Some Tag",
                         "Other Tag"
                      ],
                      "category_names":[
                         "Some Category",
                         "Another Category"
                      ],
                      "blog_id":2,
                      "comments":[
                         {
                            "id":1,
                            "post_id":1,
                            "author":"Adam",
                            "comment":"This is a valid long comment."
                         }
                      ]
                   }
                ]
             }
          ]
       }
    }
]

Model

var attr = DS.attr;

// Set up the user model and it's relationships.
EW.User = DS.Model.extend({
    first_name: attr('string'),
    last_name: attr('string'),
    user_name: attr('string'),
    blogs: DS.hasMany('blog'),
    role: DS.hasMany('roles'),

    // Create full names for every one.
    fullName: function(){
        return this.get('first_name') + ' ' + this.get('last_name');
    }.property('first_name', 'last_name')

});

Router

EW.Router.map(function(){
    this.resource('user', { path: '/user/:user_name' });
});

EW.UserRoute = Ember.Route.extend({
    model: function(params){
        return jQuery.getJSON("/user/" + params.user_name);
    }

    serialize: function(model){
        return { user_name: model.get('user_name') }
    }
});

As far as I can tell I have done everything correctly. throws the error:

Cannot GET /user/Administrator

and I am not sure if I failed to set something up properly. the console gives me no errors, the ember console wont load because of this error and the network tab is giving me a 404 for localhost:9000/user/Administrator

Ideas?

Upvotes: 0

Views: 187

Answers (1)

Jamie White
Jamie White

Reputation: 341

You’ll want to call return this.get('store').find('user', params.user_id) in your model hook:

EW.Router.map(function() {
  this.resource('user', { path: '/users/:user_id' });
});

EW.UserRoute = Ember.Route.extend({
  model: function(params) {
    return this.get('store').find('user', params.user_id);
  }
});

Note that it’s user_id rather than user_name. This is because the fixture adapter is hard coded to find records by their id rather than name. I’d suggest using the url-friendly form of the name as the ID.

Upvotes: 1

Related Questions