rails 3.2 带有 jbuilder 吞咽错误,而不是渲染丢失的部分
rails 3.2 with jbuilder swallowing errors, instead rendering missing partial
我在 JBuilder 中使用 Rails 3.2.21。
我有一个例子,我在 js.erb 文件中使用 JBuilder 部分来预填充一些字段:
var orderData = <%= raw render :partial => 'orders/orders', formats: [:json], handlers: [:jbuilder], locals: {orders: @orders} %>;
我有一个奇怪的问题,如果在 jbuilder 模板中抛出错误,它会呈现缺少模板错误。所以
如果_orders.json.jbuilder看起来像这样
json.array! orders do |order|
json.someProperty order.a_missing_property
end
我明白了:
ActionView::Template::Error (Missing partial orders/orders with {:locale=>[:en], :formats=>[:js, :html], :handlers=>[:erb, :builder, :coffee, :haml, :jbuilder, :riif]}
但如果没有错误,则可以正确呈现。
知道我的错误是如何被吞没的吗?
更新
我在这里创建了一个演示应用程序:https://github.com/earnold/error-demo
如果您加载 home/index,您会收到缺少模板的错误。如果您注释掉模板中的错误行,您将正常呈现模板。我想要做的是确保错误没有被吞噬,而是显示在页面上。
我在这里得到了关于 Github 的答案:https://github.com/rails/jbuilder/issues/40
简短版本:这可以通过猴子补丁 Rails 来解决。将其放入初始化程序中,您就可以开始了。
if Rails.env.development? || Rails.env.staging?
module ActionView
class Template
protected
def handle_render_error(view, e) #:nodoc:
if e.is_a?(Template::Error)
e.sub_template_of(self)
raise e
else
assigns = view.respond_to?(:assigns) ? view.assigns : {}
template = self
unless template.source
# If an error occurs while the Jbuilder template is being rendered in
# in a nested context, you have a mismatch between the template format
# and the view context. Therefore, this block of code would raise
# a false exception (ActionView::MissingTemplate) and swallow the original
# error. This monkey patch tricks rails into thinking it was a json request
# so that refreshing the source hits the right partial
if template.formats == [:json]
view.lookup_context.formats = [:json]
end
template = refresh(view)
template.encode!
end
raise Template::Error.new(template, assigns, e)
end
end
end
end
end
我在 JBuilder 中使用 Rails 3.2.21。
我有一个例子,我在 js.erb 文件中使用 JBuilder 部分来预填充一些字段:
var orderData = <%= raw render :partial => 'orders/orders', formats: [:json], handlers: [:jbuilder], locals: {orders: @orders} %>;
我有一个奇怪的问题,如果在 jbuilder 模板中抛出错误,它会呈现缺少模板错误。所以
如果_orders.json.jbuilder看起来像这样
json.array! orders do |order|
json.someProperty order.a_missing_property
end
我明白了:
ActionView::Template::Error (Missing partial orders/orders with {:locale=>[:en], :formats=>[:js, :html], :handlers=>[:erb, :builder, :coffee, :haml, :jbuilder, :riif]}
但如果没有错误,则可以正确呈现。
知道我的错误是如何被吞没的吗?
更新
我在这里创建了一个演示应用程序:https://github.com/earnold/error-demo
如果您加载 home/index,您会收到缺少模板的错误。如果您注释掉模板中的错误行,您将正常呈现模板。我想要做的是确保错误没有被吞噬,而是显示在页面上。
我在这里得到了关于 Github 的答案:https://github.com/rails/jbuilder/issues/40
简短版本:这可以通过猴子补丁 Rails 来解决。将其放入初始化程序中,您就可以开始了。
if Rails.env.development? || Rails.env.staging?
module ActionView
class Template
protected
def handle_render_error(view, e) #:nodoc:
if e.is_a?(Template::Error)
e.sub_template_of(self)
raise e
else
assigns = view.respond_to?(:assigns) ? view.assigns : {}
template = self
unless template.source
# If an error occurs while the Jbuilder template is being rendered in
# in a nested context, you have a mismatch between the template format
# and the view context. Therefore, this block of code would raise
# a false exception (ActionView::MissingTemplate) and swallow the original
# error. This monkey patch tricks rails into thinking it was a json request
# so that refreshing the source hits the right partial
if template.formats == [:json]
view.lookup_context.formats = [:json]
end
template = refresh(view)
template.encode!
end
raise Template::Error.new(template, assigns, e)
end
end
end
end
end