push article onto author.articles during create route

chooseauthorfromarticle
Matt Huntington 9 years ago
parent 6779ae1bac
commit aa1b4905c1

@ -12,14 +12,23 @@ 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){
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){
Article.findById(req.params.id, function(err, foundArticle){

@ -22,6 +22,12 @@
<form action="/articles" method="post">
<input type="text" name="title" /><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"/>
</form>
</main>

Loading…
Cancel
Save