秀场渲染异常如何处理?

How to process an exception during show randering?

我有以下代码:

ActiveAdmin.register Order do
  show 'rows' do
    columns do
      panel t("activerecord.models.booking.other") do
        table_for resource.bookings do
          column :from
          column :to
          column :start_at_with_timezone
          column :ticket_till
        end
      end
    end
  end
end

如果我对 bookings 有疑问,它可能是零,或者对 booking 的属性有疑问,我该如何使用 rescue

UPD:在实际代码中,我有复杂的逻辑,有时它已经崩溃,我想呈现这个异常,没有 rails 异常 Web 表单,因为在生产模式下用户会收到 500 HTTP 错误.

如果我理解你的问题 resource.bookings returns 一个可以包含 nil 作为值的 Array。示例:

resource.bookings # => [<Booking id=23>, nil, <Booking id=52>]

然后将 resource.bookings 更改为 resource.bookings.compact,这样 nil 值将被删除。

resource.bookings.compact # => [<Booking id=23>, <Booking id=52>]

然后使用rescue_from。像这样:

ActiveAdmin.register Order do
  controller do
    rescue_from MyErrorClass, with: :handle_my_error_class

    private

    def handle_my_error_class
      render 'layout/404.html' # or something
    end
  end

  show 'rows' do
    # ...
  end
end