0

I'm using Rails 3.2.21 with JBuilder.

I have an example where I'm using an a JBuilder partial inside of a js.erb file to pre populate some fields:

var orderData = <%= raw render :partial => 'orders/orders', formats: [:json], handlers: [:jbuilder], locals: {orders: @orders} %>;

I have a weird problem where if an error is thrown in the jbuilder template, it renders a missing template error. so

If _orders.json.jbuilder looks like this json.array! orders do |order| json.someProperty order.a_missing_property end

I get this: ActionView::Template::Error (Missing partial orders/orders with {:locale=>[:en], :formats=>[:js, :html], :handlers=>[:erb, :builder, :coffee, :haml, :jbuilder, :riif]}

But if there is no error, this renders properly.

Any idea how my error is getting swallowed?

Update

I've created a demo app here: https://github.com/earnold/error-demo

If you load home/index you get a missing template error. If you comment out the bad line in the template, you render the template normally. What I am trying to do is make sure that errors aren't swallowed, but instead are shown on the page.

earnold
  • 1,440
  • 1
  • 15
  • 26

1 Answers1

0

I was able to get an answer on Github here: https://github.com/rails/jbuilder/issues/40

Short version: this can be remedied by monkey patching Rails. Put this in an initializer and you're good to go.

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
earnold
  • 1,440
  • 1
  • 15
  • 26