Skip to content
This repository
Browse code

show routes while debugging RoutingError

If someone receives a routing error, they likely need to view the routes. Rather than making them visit '/rails/info/routes' or run `rake routes` we can give them that information on the page.
  • Loading branch information...
commit fa714ec7dffd12b8508c756e6526eff5ae8e4202 1 parent ee20be7
Richard Schneeman authored June 09, 2012
13  actionpack/lib/action_dispatch/middleware/debug_exceptions.rb
... ...
@@ -1,5 +1,7 @@
1 1
 require 'action_dispatch/http/request'
2 2
 require 'action_dispatch/middleware/exception_wrapper'
  3
+require 'rails/application/route_inspector'
  4
+
3 5
 
4 6
 module ActionDispatch
5 7
   # This middleware is responsible for logging exceptions and
@@ -39,7 +41,8 @@ def render_exception(env, exception)
39 41
           :exception => wrapper.exception,
40 42
           :application_trace => wrapper.application_trace,
41 43
           :framework_trace => wrapper.framework_trace,
42  
-          :full_trace => wrapper.full_trace
  44
+          :full_trace => wrapper.full_trace,
  45
+          :routes => formatted_routes(exception)
43 46
         )
44 47
 
45 48
         file = "rescues/#{wrapper.rescue_template}"
@@ -78,5 +81,13 @@ def logger(env)
78 81
     def stderr_logger
79 82
       @stderr_logger ||= ActiveSupport::Logger.new($stderr)
80 83
     end
  84
+
  85
+    private
  86
+    def formatted_routes(exception)
  87
+      if exception.is_a?(ActionController::RoutingError) || exception.is_a?(ActionView::Template::Error)
  88
+        inspector = Rails::Application::RouteInspector.new
  89
+        inspector.format(Rails.application.routes.routes).join("\n")
  90
+      end
  91
+    end
81 92
   end
82 93
 end
10  actionpack/lib/action_dispatch/middleware/templates/rescues/routing_error.erb
@@ -10,8 +10,14 @@
10 10
     </ol>
11 11
   </p>
12 12
 <% end %>
  13
+<%= render :template => "rescues/_trace" %>
  14
+
  15
+<h2>
  16
+  Routes
  17
+</h2>
  18
+
13 19
 <p>
14  
-  Try running <code>rake routes</code> for more information on available routes.
  20
+  Routes match in priority from top to bottom
15 21
 </p>
16 22
 
17  
-<%= render :template => "rescues/_trace" %>
  23
+<p><pre><%= @routes %></pre></p>

1 note on commit fa714ec

Mike Burns

Fantastic!

Please sign in to comment.
Something went wrong with that request. Please try again.