3

How can I rescue ActiveRecord::RecordNotFound in Activeadmin for all my resources?

I know in Rails I can put rescue_from(ActiveRecord::RecordNotFound) in the ApplicationController, is there an equivalent way of doing this in ActiveAdmin?

kirlev
  • 680
  • 1
  • 7
  • 17

1 Answers1

7
ActiveAdmin.register FooBar do
  controller do
    rescue_from ActiveRecord::RecordNotFound, with: :show_errors
    def show_errors
      # ...
    end
  end
end

EDIT: You can do this at one place for all resources:

require 'active_admin/base_controller'
ActiveAdmin::BaseController.class_eval do
  rescue_from ActiveRecord::RecordNotFound, with: :show_errors
  def show_errors
    # ...
  end
end
Timo Schilling
  • 3,003
  • 1
  • 17
  • 29
  • 1
    is there a way to configure that in one place for all my resources instead of duplicating that code in every resource file?? – kirlev Jul 08 '15 at 11:05
  • 1
    This didn't work for me. No matter what I do my rescue block doesn't catch errors I raise. Also PS: what variables does does show_errors have access to? – Kelsey Hannan Jul 24 '19 at 21:23