werein / x-editable-rails

Edit fields easily with X-Editable helper

Home Page:https://wereinhq.com/guides/x-editable-rails

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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:

screen shot 2013-12-02 at 10 00 47 pm

When I really want this:

screen shot 2013-12-02 at 10 01 10 pm

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.

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?