Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to send a flash message with res.redirect('/') sails js

How to send a flash message with res.redirect('/') in Sails?

When i check some condition in a controller then I want to redirect to another url, passing along a flash message.

I am new to Sails, so any help will be appreciated.

Controller action:

module.exports ={
   index: function (req, res) {

    if(req.param('key')){
      req.flash('message', 'welcome key is present');
      res.redirect('/view/');
    } else {
      req.flash('message', 'welcome key is not present');
      res.redirect('/');
    }

  }
}

Thanks in advance.

like image 535
Lucifer Avatar asked Feb 13 '14 05:02

Lucifer


Video Answer


2 Answers

Your code looks fine for the controller. In your view, you can access the flash message as req.flash('message'), so in an .ejs file for example it would be <%- req.flash('message') %>

like image 157
sgress454 Avatar answered Sep 23 '22 04:09

sgress454


What I find better for any redirect with data, is to set the http code to 307. It'll redirect with post/put/delete data.

req.flash('message');
res.redirect(307, '/');
like image 28
jemiloii Avatar answered Sep 26 '22 04:09

jemiloii