I am trying to use flash('error', 'error text')
to alert the webpage that an error has occurred via an ajax request. The ajax request hits an action where some database work is involved, and an error could be produced.
Controller:
load('application');
action('index', function() {
this.title = 'Sample Page';
render();
});
action('test', function() {
flash('error', 'test error message');
render('index', { title: 'Sample Page' });
});
Sample ajax call:
$.ajax({
url: '/test-error',
success: function(response) {
console.log(response);
},
error: function(response) {
console.log(response);
}
});
Routes:
map.get('test', 'test-error#index');
map.get('test-error', 'test-error#test');
Is this even possible through an ajax call? I've tried using flash
, followed by render('index')
as shown above and have tried redirect(path_to.test);
with no success. send(500, 'error message');
returns an error to the ajax call, which, if necessary, I could reload the page from there.
I ended up using using the
send
method to return errors to the page, and then I created aflash
function in javascript which flashes similar to how the server produces the message, and uses ICanHaz.js for generating the HTML.In app/views/layouts/application_layout.ejs, I modified the div that contains the alerts to have the id
flashMessage
:Mustache template (for ICanHaz, ich.flash):
Javascript flash function: