From b5fc4a024f4e7a620bfb702bd33f01900bed1ea4 Mon Sep 17 00:00:00 2001 From: Sahat Yalkabov Date: Sat, 30 Nov 2013 13:27:43 -0500 Subject: [PATCH] Success and error flash messages on contact form --- controllers/contact.js | 22 +++++++++++++++------- views/contact.jade | 8 +++++--- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/controllers/contact.js b/controllers/contact.js index 7a15240e46..10e66f1798 100644 --- a/controllers/contact.js +++ b/controllers/contact.js @@ -5,7 +5,8 @@ exports.getContact = function(req, res) { res.render('contact', { title: 'Contact', user: req.user, - messages: req.flash('messages') + success: req.flash('success'), + error: req.flash('error') }); }; @@ -15,13 +16,20 @@ exports.postContact = function(req, res) { var email = req.body.email; var body = req.body.contactBody; + var sendTo = 'sakhat@gmail.com'; + var subject = 'API Example | Contact Form'; + sendgrid.send({ - to: 'example@example.com', - from: 'other@example.com', - subject: 'Hello World', - text: 'My first email through SendGrid.' + to: sendTo, + from: email, + subject: subject, + text: body }, function(err, json) { - if (err) { return console.error(err); } - console.log(json); + if (err) { + req.flash('error', err.message); + return res.redirect('/contact'); + } + req.flash('success', 'Email has been sent successfully!'); + res.redirect('/contact'); }); }; \ No newline at end of file diff --git a/views/contact.jade b/views/contact.jade index 74c4ed47a3..b20aca5480 100644 --- a/views/contact.jade +++ b/views/contact.jade @@ -25,7 +25,9 @@ block content i.fa.fa-location-arrow | Send button.btn.btn-link(disabled='') Powered by SendGrid - if messages.length + if error.length .alert.alert-danger - for message in messages - div= message \ No newline at end of file + p= error + if success.length + .alert.alert-success + p= success