我的 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
。我试过调试分配
,但总是{}
。调用 render :template
时不会复制赋值吗?如果是这样,我怎样才能得到它们?
我在边缘 Rails。
最佳答案
这很奇怪,我不知道为什么。作为替代方案,您是否尝试过将异常作为显式本地传递?
def not_found(exception)
render :template => '/errors/not_found',
:status => 404,
:locals => {:exception => exception}
end
和 View :
<h1>Error 404: Not Found</h1>
<%= debug exception %> <!-- Note no '@' -->
关于ruby-on-rails - 如何在 "assigns"之后访问 "render :template => ..."?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/209753/