↖️ Show all posts

Enforce Error Callback in an AJAX Call from controller method in Rails

When hitting the controller with an AJAX request, maybe some DB requests run and some data is processed. Now let’s assume you are getting content with that AJAX call. Wouldn’t it be much cleaner for your views to only render if your controller returns the desired data and otherwise make the AJAX call error out? Sometimes, absolutely!

# welcome_controller.rb
def someroute
  @friend = Friend.find(params[:id])
  return head :bad_request unless @friend
end

The AJAX call fails and runs its error callback, due to the Status 400 that was sent. More details in the Rails Docs


⬅️ Read previous Read next ➡️