ruby on rails - Return error as a JSON response -
the question simple: return error controller json response:
//controller def action1 # ..... return render(json: { status: :unprocessable_entity }) if !is_my_valid? # .... doing render(json: my_hash.to_json) end
//view $.ajax({ url: 'some_url', //... }).done(function(data) { // gets here in case of error }).fail(function(jqxhr, textstatus) { alert("error: " + textstatus); });
so problem on html page, in case of error, callback function in .done()
executed, not 1 in .fail()
. how make .fail()
?
send 500 http status code:
render(json: { ... }, status: 500)
Comments
Post a Comment