I have a simple Rails app (https://github.com/hqmq/comic_collector) which has nested resources:
resources :books do
resources :issues
end
I have set up two Ember models for these resources and am trying to create a new issue. Here are my models (using ember-tools):
var Book = DS.Model.extend({
name: DS.attr('string'),
cover_image_url: DS.attr('string'),
created_at: DS.attr('date'),
updated_at: DS.attr('date'),
issues: DS.hasMany('App.Issue', { embedded: true }),
totalIssues: function() {
return this.get('issues').get('length');
}.property('issues.@each'),
totalPrice: function() {
return this.get('issues').getEach('price').reduce(function(accum, price) {
return (Math.round(accum * 100) + Math.round(price * 100)) / 100;
})
}.property('issues.@each')
});
module.exports = Book;
var Issue = DS.Model.extend({
number: DS.attr('number'),
title: DS.attr('string'),
cover_image_url: DS.attr('string'),
price: DS.attr('number'),
created_at: DS.attr('date'),
updated_at: DS.attr('date'),
book: DS.belongsTo('App.Book')
});
module.exports = Issue;
Here’s the controller which handles the post of a new issue:
var BookController = Ember.ObjectController.extend({
createIssue: function() {
var data = {
book_id: this.get('model.id'),
cover_image_url: this.get('new_cover_image_url'),
number: this.get('new_number'),
price: this.get('new_price'),
title: this.get('new_title'),
}
var issues = this.get('model').get('issues');
var newIssue = issues.createRecord(data);
this.get('store').commit();
}
});
module.exports = BookController;
When the createIssue function is called, it posts to /issues instead of /books/1/issues.
Can you please help? I’m sure the solution is easy, I’m just not seeing anything in the docs or blog posts.
Eric