如何为我的所有资源抢救Activeadmin中的ActiveRecord :: RecordNotFound?

我知道在Rails中可以将rescue_from(ActiveRecord::RecordNotFound)放在ApplicationController中,在ActiveAdmin中是否有等效的方法?

最佳答案

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


编辑:
您可以在一处为所有资源执行此操作:

require 'active_admin/base_controller'
ActiveAdmin::BaseController.class_eval do
  rescue_from ActiveRecord::RecordNotFound, with: :show_errors
  def show_errors
    # ...
  end
end

09-25 17:04