The question is simple: return an error from a controller as JSON response:
//controller
def action1
# .....
return render(json: { status: :unprocessable_entity }) if !is_my_valid?
# .... doing something
render(json: my_hash.to_json)
end
//view
$.ajax({
url: 'some_url',
//...
}).done(function(data) {
// it always gets here even in case of an error
}).fail(function(jqXHR, textStatus) {
alert("error: " + textStatus);
});
So the problem is that on the HTML page, even in case of an error, the callback function in .done()
is executed, not the one in .fail()
. How do I make it into .fail()
?