From 37d9c44b6d15cc0b02f327fe51687db7d0443bb8 Mon Sep 17 00:00:00 2001 From: schneems Date: Mon, 17 Dec 2012 14:26:46 -0600 Subject: [PATCH] Format routes as html on debug page When someone gets a routing exception, the routes are rendered (starting in Rails 4.0). This PR brings parity between the html routes in the `rails/info/routes` path and when rendered from an exception. This is the continuation of #8521 which brought html formatted routes. In addition to bringing parity to the two views, we're keeping our views DRY by rendering off of the same partials. In this case Railties depends on partials provided by ActionDispatch. I'm open to alternative implementations. Ideally both views will use the same code so any improvements or updates to it will be reproduced on both.
![](http://f.cl.ly/items/3O1D0K1v0j0i343O3T3T/Screen%20Shot%202012-12-17%20at%203.07.20%20PM.png) --- .../middleware/debug_exceptions.rb | 2 +- .../templates/rescues/routing_error.erb | 4 +- .../templates/routes/_route.html.erb | 16 ++++ .../templates/routes/_route_wrapper.html.erb | 56 +++++++++++++ railties/lib/rails/info_controller.rb | 1 + .../templates/rails/info/routes.html.erb | 78 +------------------ 6 files changed, 81 insertions(+), 76 deletions(-) create mode 100644 actionpack/lib/action_dispatch/middleware/templates/routes/_route.html.erb create mode 100644 actionpack/lib/action_dispatch/middleware/templates/routes/_route_wrapper.html.erb diff --git a/actionpack/lib/action_dispatch/middleware/debug_exceptions.rb b/actionpack/lib/action_dispatch/middleware/debug_exceptions.rb index f356c2a85c2e4..1dc51d62e058e 100644 --- a/actionpack/lib/action_dispatch/middleware/debug_exceptions.rb +++ b/actionpack/lib/action_dispatch/middleware/debug_exceptions.rb @@ -87,7 +87,7 @@ def formatted_routes(exception) return false unless @routes_app.respond_to?(:routes) if exception.is_a?(ActionController::RoutingError) || exception.is_a?(ActionView::Template::Error) inspector = ActionDispatch::Routing::RoutesInspector.new - inspector.format(@routes_app.routes.routes).join("\n") + inspector.collect_routes(@routes_app.routes.routes) end end end diff --git a/actionpack/lib/action_dispatch/middleware/templates/rescues/routing_error.erb b/actionpack/lib/action_dispatch/middleware/templates/rescues/routing_error.erb index a357a7ba11479..6c903d6a1743d 100644 --- a/actionpack/lib/action_dispatch/middleware/templates/rescues/routing_error.erb +++ b/actionpack/lib/action_dispatch/middleware/templates/rescues/routing_error.erb @@ -20,4 +20,6 @@ Routes match in priority from top to bottom

-

<%= @routes %>

+<%= render layout: "routes/route_wrapper" do %> + <%= render partial: "routes/route", collection: @routes %> +<% end %> diff --git a/actionpack/lib/action_dispatch/middleware/templates/routes/_route.html.erb b/actionpack/lib/action_dispatch/middleware/templates/routes/_route.html.erb new file mode 100644 index 0000000000000..400ae97d22cec --- /dev/null +++ b/actionpack/lib/action_dispatch/middleware/templates/routes/_route.html.erb @@ -0,0 +1,16 @@ + + + <% if route[:name].present? %> + <%= route[:name] %>_path + <% end %> + + + <%= route[:verb] %> + + + <%= route[:path] %> + + + <%= route[:reqs] %> + + diff --git a/actionpack/lib/action_dispatch/middleware/templates/routes/_route_wrapper.html.erb b/actionpack/lib/action_dispatch/middleware/templates/routes/_route_wrapper.html.erb new file mode 100644 index 0000000000000..9bf5c96c954e7 --- /dev/null +++ b/actionpack/lib/action_dispatch/middleware/templates/routes/_route_wrapper.html.erb @@ -0,0 +1,56 @@ + + + + + + + + + + + + + <%= yield %> + +
Helper
+ <%= link_to "Path", "#", 'data-route-helper' => '_path', + title: "Returns a relative path (without the http or domain)" %> / + <%= link_to "Url", "#", 'data-route-helper' => '_url', + title: "Returns an absolute url (with the http and domain)" %> +
HTTP VerbPathController#Action
+ + diff --git a/railties/lib/rails/info_controller.rb b/railties/lib/rails/info_controller.rb index ec7d5ed610f08..e650f58d20439 100644 --- a/railties/lib/rails/info_controller.rb +++ b/railties/lib/rails/info_controller.rb @@ -2,6 +2,7 @@ class Rails::InfoController < ActionController::Base # :nodoc: self.view_paths = File.expand_path('../templates', __FILE__) + prepend_view_path ActionDispatch::DebugExceptions::RESCUES_TEMPLATE_PATH layout 'application' before_filter :require_local! diff --git a/railties/lib/rails/templates/rails/info/routes.html.erb b/railties/lib/rails/templates/rails/info/routes.html.erb index e34c1c41357dd..1ea387c63f2b9 100644 --- a/railties/lib/rails/templates/rails/info/routes.html.erb +++ b/railties/lib/rails/templates/rails/info/routes.html.erb @@ -6,77 +6,7 @@ Routes match in priority from top to bottom

- - - - - - - - - - - - - <% @routes.each do |route| %> - - - - - - - <% end %> - -
Helper
- <%= link_to "Path", "#", 'data-route-helper' => '_path', - title: "Returns a relative path (without the http or domain)" %> / - <%= link_to "Url", "#", 'data-route-helper' => '_url', - title: "Returns an absolute url (with the http and domain)" %> -
HTTP VerbPathController#Action
- <% if route[:name].present? %> - <%= route[:name] %>_path - <% end %> - - <%= route[:verb] %> - - <%= route[:path] %> - - <%= route[:reqs] %> -
- - +<%# actionpack/lib/action_dispatch/middleware/templates %> +<%= render layout: "routes/route_wrapper" do %> + <%= render partial: "routes/route", collection: @routes %> +<% end %>