EDIT: See the Sails documentation here for more information - basically, since you use static routing, the view policies are not applied before rendering, so the flash policy does not work. I would recommend adding a register action to your UserController and just call res.view() from there. There's also fooobar.com/questions/1182926 / ... which discusses this if you want more information.
I have an alternative that I developed for my own project, which you can try (also requires non-static routing).
In your api/policies folder, create a flash.js policy:
// flash.js policy module.exports = function(req, res, next) { res.locals.messages = { success: [], error: [], warning: [] }; if(!req.session.messages) { req.session.messages = { success: [], error: [], warning: [] }; return next(); } res.locals.messages = _.clone(req.session.messages); // Clear flash req.session.messages = { success: [], error: [], warning: [] }; return next(); };
This policy allows you to use three different types of flash memory: success, warning, and errors. It will create an empty dictionary for each session and clear it when the page loads.
I created the FlashService.js service in api/services for lighter flash messages:
// FlashService.js module.exports = { success: function(req, message) { req.session.messages['success'].push(message); }, warning: function(req, message) { req.session.messages['warning'].push(message); }, error: function(req, message) { req.session.messages['error'].push(message); } }
Then, in the config/policies.js configuration file, be sure to assign a flash policy to the controller actions that you want to use with:
// config/policies.js module.exports.policies = { '*': [true, 'flash'], 'UserController': { 'register': ['flash'], // any future actions that want flash }, 'AnotherController': { 'someAction': ['flash', 'somePolicy'], } }
To execute a flash message in a register action, simply use FlashService.success(req, message) . Alternatively, you can use FlashService.error and FlashService.warning , depending on how your user interface style works and how much you want to distinguish your flash messages.
In your opinion, you can add something like this:
<% if (messages && messages['error'].length > 0) { %> <div class="alert alert-danger"> <% messages['error'].forEach(function(message) { %> <%= message %> <br> <% }); %> </div> <br> <% } %> <% if (messages && messages['warning'].length > 0) { %> <div class="alert alert-warning"> <% messages['warning'].forEach(function(message) { %> <%= message %> <br> <% }); %> </div> <br> <% } %> <% if (messages && messages['success'].length > 0) { %> <div class="alert alert-success"> <% messages['success'].forEach(function(message) { %> <%= message %> <br> <% }); %> </div> <br> <% } %>
Of course, you will have to change the div classes to all classes relevant to your user interface.