4

我的 ApplicationController 中有一个错误处理方法:

rescue_from ActiveRecord::RecordNotFound, :with => :not_found

def not_found(exception)
  @exception = exception
  render :template => '/errors/not_found', :status => 404
end

RAILS_ROOT/app/views/errors/not_found.html.erb,我有这个:

<h1>Error 404: Not Found</h1>
<%= debug @exception %>

@exception一直都nil在。我试过debug assigns了,但总是这样{}。调用时分配不会被复制render :template吗?如果是这样,我怎样才能得到它们?

我在边缘 Rails 上。

4

2 回答 2

5

这很奇怪,我不知道为什么。作为替代方案,您是否尝试过将异常作为显式本地传递?

def not_found(exception)
  render :template => '/errors/not_found', 
         :status   => 404, 
         :locals   => {:exception => exception}
end

和观点:

<h1>Error 404: Not Found</h1>
<%= debug exception %> <!-- Note no '@' -->
于 2008-10-16T18:52:53.700 回答
1

ActionController::Base的 API 文档看来,您应该尝试:

render :template => '/errors/not_found', :status => 404, :locals => {:exception => exception}
于 2008-10-16T18:54:54.197 回答