Sails.js - How to render a partial from a controller?

Samuel Poirier picture Samuel Poirier · Aug 15, 2013 · Viewed 9.9k times · Source

I have a partial view containing my login form. I would like to render it from an ajax call to my controller.

This is the sample where i would return my partial view:

postlogin: function (req,res) {

    var username = req.param('username');
    var password = req.param('password');

    User.find({
        username: username,
        password: password.salt()
    }).done(function(err, users){
        if(users.length == 1){
            // Here I want to return a partial view, not a view
            res.view('home/login', {message: 'Login success!'});
        }else{
            // Here I want to return a partial view, not a view
            res.view('home/login', {message: 'Login failed!'});
        }
    });
  },

Answer

Samuel Poirier picture Samuel Poirier · Aug 16, 2013

Ah! Found it!

If your view is a partial view, simply specify layout: null:

res.view('home/login', {message: 'Login failed!', layout: null});