Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
36 lines (26 sloc) 764 Bytes
title nav_order
Error Handling
76

Jets provides some error handling capabilities in controllers that can rescue errors that occur during the callbacks or action. This is done with the rescue_from method.

Example rescue_from

class PostsController < ApplicationController
  rescue_from ActiveRecord::RecordNotFound do |exception|
    render json: { message: "We could not find your post." }, status: 404
  end

  # ...
end

Example using with association

class PostsController < ApplicationController
  rescue_from ActiveRecord::RecordNotFound, with: :missing_post
  
  # ...
  
private
  def missing_post
    render json: { message: "We could not find your post." }, status: 404
  end
end

{% include prev_next.md %}

You can’t perform that action at this time.