Skip to content

Commit

Permalink
Hook routes into post model
Browse files Browse the repository at this point in the history
This acceptance test error drives a lot of logic: to display the post's title on the show page, we need to save the post on the new page, include the ID in the transition to the show route, then load the post on the show page's model hook.

Outer red: Uncaught TypeError: Cannot read property 'create' of undefined

This isn't a very descriptive error, but it seems to be related to the fact that there _is_ no `post` model.
  • Loading branch information
Josh Justice authored and CodingItWrong committed Sep 6, 2018
1 parent 8fc9f21 commit 075712b
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 3 deletions.
6 changes: 4 additions & 2 deletions app/controllers/posts/new.js
Expand Up @@ -2,8 +2,10 @@ import Controller from '@ember/controller';

export default Controller.extend({
actions: {
savePost() {
this.transitionToRoute('posts.show');
async savePost(postData) {
let post = this.store.createRecord('post', postData);
await post.save();
this.transitionToRoute('posts.show', post.id);
}
}
});
2 changes: 1 addition & 1 deletion app/router.js
Expand Up @@ -9,7 +9,7 @@ const Router = EmberRouter.extend({
Router.map(function() {
this.route('posts', function() {
this.route('new');
this.route('show');
this.route('show', { path: ':id' });
});
});

Expand Down
3 changes: 3 additions & 0 deletions app/routes/posts/show.js
@@ -1,4 +1,7 @@
import Route from '@ember/routing/route';

export default Route.extend({
model({ id }) {
return this.store.findRecord('post', id);
}
});

0 comments on commit 075712b

Please sign in to comment.