Feature request: better default parsing of Rails error responses
bteitelb opened this issue · comments
When returning Rails validation errors in the usual way:
format.json { render json: @obj.errors, status: :unprocessable_entity }
I get something like this:
When I really want this:
To get that, I've written a default error handler to massage the JSONified Rails validation errors:
$.fn.editable.defaults.error =
function(response, newValue) {
var field_name = $(this).data('name'),
error_msgs = response.responseJSON[field_name];
return error_msgs.join("; ");
};
Should something like this be built into x-editable-rails?
Hi, you're right, your error handling looks much better, I'll update it with your solution in few minutes. Thanks! :)
Render whole object in JSON, not only object.errors, because when you use respond_with, it use whole object too.
Would be nice to see something in the README about how to properly render errors for x-editable-rails. I gave up on rest_in_place because I didn't see anything in the docs about it.
@odigity 👍
I wrote this method to show all object errors
$('.editable').editable({
mode: 'inline',
showbuttons: false,
error: function(response, newValue) {
var error_msgs = []
$.each(response.responseJSON, function(key, value){
error_msgs.push(key+' '+value)
})
return error_msgs.join("; ");
}
})
@bperucchi your solution worked nicely for me. Have to say this was the biggest stumbling block for me when setting up x-editable-rails. I think the gem, and it's docs need to do a better job of explaining how they expect errors to be returned from the controller.
@jirikolarik could you explain what format the error response should come in? Both full object and the error messages? If so, how are you rendering that in your controllers?