Search code examples
jqueryruby-on-railscoffeescriptbest-in-place

unable to handle HTTP 422 with best in place rails


I have my controller code like this:

def update
respond_to do |format|
  if refresh_quantity_in_inventory
    format.json { render json: { message: "Quantity successfully updated" } }
  else
    format.json { render json: @errors, status: :unprocessable_entity }
  end
end

end

The CoffeeScript for this is like this:

ready = ->
   $('#best_in_place_cart_quantity').best_in_place().bind 'ajax:error', (evt, data, status, xhr) ->
      alert xhr
      alert xhr.status
      if (xhr.status == 422)
        alert("Inventory count is not enough")
$(document).ready ready
$(document).on "page:load", ready

I have also tried ajax:success but neither triggers the callback. How do I catch 422? What should I do?


$('#best_in_place_cart_quantity').best_in_place().done((data, status, response) ->
    alert 'Done handler. HTTP code: ' + response.status
    return
  ).fail (error) ->
    alert 'Fail handler. HTTP code: ' + error.status
    return

This did not work either.


Solution

  • This is the code that finally worked.

    $('.best_in_place_cart_quantity').best_in_place()
    
    $(".best_in_place_cart_quantity").bind 'ajax:error',  (evt,  data,  status,  xhr) ->
        alert "Not enough quantity"
    

    Lesson learnt was that, we should bind best in place fields through class instead of Id.