【发布时间】:2016-08-01 20:11:45
【问题描述】:
我有一个 ERB 模板,其中包含一些来自其他 gem 的辅助方法
sample.html.erb
<h1>All The Stuff</h1>
<% all_the_stuff.each do |stuff| %>
<div><%= stuff %></div>
<% end %>
lib/random_file.rb
def all_the_stuff
if ALL_THE_STUFF.exists?
ALL_THE_STUFF
else
fail AllTheStuffException
end
end
现在,如果 ALL_THE_STUFF 不存在,我会得到一个 ActionView::Template::Error。但是,在控制器级别的异常处理不会被捕获。
在控制器的情况下,我使用rescue_from 挽救ApplicationController 中的异常。有没有一个地方可以存放我所有的 ERB 模板?
如何处理在模板中捕获的异常(不是由于控制器代码)?
【问题讨论】:
-
begin...rescue...end在 ERB 中工作得很好。 -
我应该更清楚。对于我的应用程序控制器,我可以执行
rescue_from块来救援,但没有这样的地方可以为所有模板执行此操作。 -
你应该能够以同样的方式做到这一点:rescue_from ActionView::Template::Error, with: :your_method_here
标签: ruby-on-rails ruby