在Activeadmin中救援RecordNotFound

时间:2015-06-15 11:55:38

标签: ruby-on-rails ruby-on-rails-4 activeadmin inherited-resources

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

我知道在Rails中我可以将rescue_from(ActiveRecord::RecordNotFound)放在ApplicationController中,在ActiveAdmin中有相同的方法吗?

1 个答案:

答案 0 :(得分:6)

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