我如何访问“分配”之后“渲染:template => …&#8221 ;?
-
03-07-2019 - |
题
我的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边缘。
解决方案
这很奇怪,我不知道为什么。作为替代方案,您是否尝试将异常作为显式本地传递?
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 '@' -->
其他提示
从 ActionController :: Base 的API文档中查找你应该尝试:
render :template => '/errors/not_found', :status => 404, :locals => {:exception => exception}
不隶属于 StackOverflow