2015-05-02 23 views
0

我使用Rails 3.2.21與JBuilder。rails 3.2與jbuilder吞嚥錯誤,而不是渲染丟失部分

我有哪裏我使用的是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

如果加載家裏,你得到一個丟失的模板錯誤/索引。如果您註釋掉模板中的壞行,則通常會呈現模板。我想要做的是確保錯誤不被吞噬,而是顯示在頁面上。

回答

0

我能在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