From aa1b4905c17bfdc4c0f6ab599ef5cbee23e7ee26 Mon Sep 17 00:00:00 2001 From: Matt Huntington Date: Fri, 25 Nov 2016 03:37:46 +0100 Subject: [PATCH] push article onto author.articles during create route --- controllers/articles.js | 17 +++++++++++++---- views/articles/new.ejs | 6 ++++++ 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/controllers/articles.js b/controllers/articles.js index ab8ca89..efd48ee 100644 --- a/controllers/articles.js +++ b/controllers/articles.js @@ -12,13 +12,22 @@ router.get('/', function(req, res){ }); router.get('/new', function(req, res){ - res.render('articles/new.ejs'); + Author.find({}, function(err, allAuthors){ + res.render('articles/new.ejs', { + authors:allAuthors + }); + }); }); router.post('/', function(req, res){ - Article.create(req.body, function(err, createdArticle){ - res.redirect('/articles'); - }) + Author.findById(req.body.authorId, function(err, foundAuthor){ + Article.create(req.body, function(err, createdArticle){ + foundAuthor.articles.push(createdArticle); + foundAuthor.save(function(){ + res.redirect('/articles'); + }) + }); + }); }); router.get('/:id', function(req,res){ diff --git a/views/articles/new.ejs b/views/articles/new.ejs index 4eb560c..b97714c 100644 --- a/views/articles/new.ejs +++ b/views/articles/new.ejs @@ -22,6 +22,12 @@


+ Choose and author: +