push article onto author.articles during create route

chooseauthorfromarticle
Matt Huntington 9 years ago
parent 6779ae1bac
commit aa1b4905c1

@ -12,13 +12,22 @@ router.get('/', function(req, res){
}); });
router.get('/new', 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){ router.post('/', function(req, res){
Article.create(req.body, function(err, createdArticle){ Author.findById(req.body.authorId, function(err, foundAuthor){
res.redirect('/articles'); Article.create(req.body, function(err, createdArticle){
}) foundAuthor.articles.push(createdArticle);
foundAuthor.save(function(){
res.redirect('/articles');
})
});
});
}); });
router.get('/:id', function(req,res){ router.get('/:id', function(req,res){

@ -22,6 +22,12 @@
<form action="/articles" method="post"> <form action="/articles" method="post">
<input type="text" name="title" /><br/> <input type="text" name="title" /><br/>
<textarea name="body"></textarea><br/> <textarea name="body"></textarea><br/>
Choose and author:
<select name="authorId">
<% for(var i = 0; i < authors.length; i++) { %>
<option value="<%=authors[i]._id %>"><%=authors[i].name %></option>
<% } %>
</select><br/>
<input type="submit" value="Publish Article"/> <input type="submit" value="Publish Article"/>
</form> </form>
</main> </main>

Loading…
Cancel
Save