【发布时间】:2025-12-14 11:40:01
【问题描述】:
对于 show 操作,在未捕获的异常上返回 500 错误很好,但对于 index 操作,如果单个错误资源不会导致整个请求失败,这将很有帮助。有没有办法从这些异常中解救出来并返回剩余的资源以及错误列表?
详细信息:我正在使用 RABL 来呈现这样的 JSON 模板(但我认为解决方案可能是通用的,而不是针对此的):
# app/controllers/happenings_controller.rb
def index
@happenings = current_person.happenings
end
# app/views/happening/index.json.rabl
collection @happenings
extends 'happenings/show'
# app/views/happening/show.json.rabl
object @happening
attributes :id, :name, :description
node :creator, if: lambda { |s| s.creator? } do |s|
# !!! This is where an exception on a single resource was blowing up the whole request
partial("people/show", :object => s.creator)
end
【问题讨论】:
-
定义“坏资源”?控制器中的 index 动作是什么样的?
-
坏资源是渲染引发异常的资源。我正在使用 RABL 模板自动呈现每个资源的显示模板,但可以手动执行此操作,因此我认为解决方案不必考虑这一点。详细信息已添加到帖子中。
标签: ruby-on-rails