Skip to content

Instantly share code, notes, and snippets.

@lukehorvat
Created September 1, 2013 12:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lukehorvat/6404062 to your computer and use it in GitHub Desktop.
Save lukehorvat/6404062 to your computer and use it in GitHub Desktop.
List all of the routes in your Ruby on Rails application in a nicely-formatted HTML table. Well-suited to those times when you're building an API and need an "auto-updating" reference page of application endpoints that you can share with others.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Routes</title>
<link href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<h1 style="text-align: center; margin-bottom: 20px;">Routes</h1>
<table class="table table-striped table-hover">
<thead>
<tr>
<th style="width: 45%;">Path</th>
<th style="width: 10%;">Verb</th>
<th style="width: 45%;">Controller#Action</th>
</tr>
</thead>
<tbody>
<% Rails.application.routes.routes.each do |route| %>
<% path = route.path.ast() %>
<% verb = route.verb.source %>
<% verb = verb[1..(verb.length-2)] if verb %>
<% controller = route.requirements[:controller] %>
<% action = route.requirements[:action] %>
<tr>
<td><%= path %></td>
<td><%= verb %></td>
<td><%= controller %>&#35;<%= action %></td>
</tr>
<% end %>
</tbody>
</table>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment