Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.