I am always forced to make my terminal window two dual monitors wide just to see read them开发者_开发技巧 right. I'm not a stickler for buttery GUI's, but this is ridiculous.
Is there a pretty print for this command?
EDIT: The answer below was packaged into the html_routes gem which supports Rails 3 and 4.
The code below was made with the current Rails 3.2.3, groups by controller and looks awesome. Remember to change the <Your APP>
to your app name and add gem 'syntax' to your Gemfile.
desc 'Pretty print out all defined routes in match order, with names. Target specific controller with CONTROLLER=x.'
task :routes => :environment do
if ENV['CONTROLLER']
all_routes = <Your APP>::Application.routes.select { |route| route.defaults[:controller] == ENV['CONTROLLER'] }
else
all_routes = <Your APP>::Application.routes
end
convertor = Syntax::Convertors::HTML.for_syntax "ruby"
File.open(File.join(Rails.root, "routes.html"), "w") do |f|
f.puts "<html><head><title>Your APP</title>
<style type='text/css'>
body { background-color: #333; color: #FFF; }
table { border: 1px solid #777; background-color: #111; }
td, th { font-size: 11pt; text-align: left; padding-right: 10px; }
th { font-size: 12pt; }
pre { maring: 0; padding: 0; }
.contrl_head { font-size: 14pt; padding: 15px 0 5px 0; }
.contrl_name { color: #ACE; }
.punct { color: #99F; font-weight: bold; }
.symbol { color: #7DD; }
.regex { color: #F66; }
.string { color: #F99; }4
</style></head>
<body>"
last_contrl = nil
routes = all_routes.routes.collect do |route|
if !route.requirements.empty?
if route.requirements[:controller] != last_contrl
f.puts "</table>" if last_contrl
last_contrl = route.requirements[:controller]
f.puts "<div class='contrl_head'><b>Controller: <span class='contrl_name'>#{last_contrl}</span></b></div>" +
"<table width='100%' border='0'><tr><th>Name</th><th>Verb</th><th>Path</th><th>Requirements</th></tr>"
end
reqs = route.requirements.inspect
verb = route.verb.source
verb = verb[1..(verb.length-2)] if verb
r = { :name => route.name, :verb => verb, :path => route.path, :reqs => reqs }
f.puts "<tr><td width='12%'><b>#{r[:name]}</b></td><td width='5%'><b>#{r[:verb]}</b></td>" +
"<td width='3%'>#{r[:path]}</td><td width='80%'>#{convertor.convert(r[:reqs])}</td></tr>"
end
end
f.puts "</table></body></html>"
end
end
https://github.com/nicooga/color_routes does it pretty well, I think!
Use the in-built display given by rails
- Start the server
rails s
- Click on this link: http://localhost:3000/rails/info/routes - this should look a LOT better than in the terminal + you can search through the routes too (which is very handy). Personally, I have saved it as a bookmark.
- Voila:
Rails 3.1 version, Replace all <YourApp> tag with your application name.
desc 'Pretty print out all defined routes in match order, with names. Target specific controller with CONTROLLER=x.'
task :pretty_routes => :environment do
all_routes = ENV['CONTROLLER'] ? <YourApp>::Application.routes.select { |route| route.defaults[:controller] == ENV['CONTROLLER'] } : <YourApp>::Application.routes
routes = all_routes.routes.collect do |route|
reqs = route.requirements.empty? ? "" : route.requirements.inspect
{:name => route.name, :verb => route.verb, :path => route.path, :reqs => reqs}
end
File.open(File.join(Rails.root, "routes.html"), "w") do |f|
f.puts "<html><head><title>Rails 3 Routes</title></head><body><table border=1>"
f.puts "<tr><th>Name</th><th>Verb</th><th>Path</th><th>Requirements</th></tr>"
routes.each do |r|
f.puts "<tr><td>#{r[:name]}</td><td>#{r[:verb]}</td><td>#{r[:path]}</td><td>#{r[:reqs]}</td></tr>"
end
f.puts "</table></body></html>"
end
end
I've rewritten the rake routes command slightly to generate a slightly more usable html version of the rake routes output
Create a file pretty_routes.rake
and put this in lib/tasks/
and call rake pretty_routes
and it should be slightly better
desc 'Pretty print out all defined routes in match order, with names. Target specific controller with CONTROLLER=x.'
task :pretty_routes => :environment do
all_routes = ENV['CONTROLLER'] ? ActionController::Routing::Routes.routes.select { |route| route.defaults[:controller] == ENV['CONTROLLER'] } : ActionController::Routing::Routes.routes
routes = all_routes.collect do |route|
name = ActionController::Routing::Routes.named_routes.routes.index(route).to_s
verb = route.conditions[:method].to_s.upcase
segs = route.segments.inject("") { |str,s| str << s.to_s }
segs.chop! if segs.length > 1
reqs = route.requirements.empty? ? "" : route.requirements.inspect
{:name => name, :verb => verb, :segs => segs, :reqs => reqs}
end
File.open(File.join(RAILS_ROOT, "routes.html"), "w") do |f|
f.puts "<html><head><title>Rails Routes</title></head><body><table border=1>"
f.puts "<tr><th>Name</th><th>Verb</th><th>Segments</th><th>Requirements</th></tr>"
routes.each do |r|
f.puts "<tr><td>#{r[:name]}</td><td>#{r[:verb]}</td><td>#{r[:segs]}</td><td>#{r[:reqs]}</td></tr>"
end
f.puts "</table></body></html>"
end
`open #{File.join(RAILS_ROOT, "routes.html")}`
end
The second to last line only works on Mac OSX an in rails 2.x, but it automatically opens the file in your browser. If you are on a different platform, you will have to change the command.
If you are running Rails 3.x, the second to last line should be
`open #{File.join(Rails.root, "routes.html")}`
Rails 6+
For those, who anyway want to use their terminal to view complicated rails routes
.
Rails 6 introduced --expanded
option to print them in a more convenient way.
Here is an example:
$ rails routes --expanded
--[ Route 1 ]------------------------------------------------------------
Prefix | high_scores
Verb | GET
URI | /high_scores(.:format)
Controller#Action | high_scores#index
--[ Route 2 ]------------------------------------------------------------
Prefix | new_high_score
Verb | GET
URI | /high_scores/new(.:format)
Controller#Action | high_scores#new
--[ Route 3 ]------------------------------------------------------------
Prefix | blog
Verb |
URI | /blog
Controller#Action | Blog::Engine
[ Routes for Blog::Engine ]
--[ Route 1 ]------------------------------------------------------------
Prefix | cart
Verb | GET
URI | /cart(.:format)
Controller#Action | cart#show
And a link to PR.
You could use Sextant to print the routes in your browser: https://github.com/schneems/sextant
For Rails 3, you can use : Rails.application.routes.routes.to_a
(see my original answer)
Great tip. Thanks.
I prepared working version for Rails 3.0. Enjoy.
desc 'Pretty print out all defined routes in match order, with names. Target specific controller with CONTROLLER=x.'
task :pretty_routes => :environment do
all_routes = ENV['CONTROLLER'] ? ActionController::Routing::Routes.routes.select { |route| route.defaults[:controller] == ENV['CONTROLLER'] } : ActionController::Routing::Routes.routes
routes = all_routes.collect do |route|
reqs = route.requirements.empty? ? "" : route.requirements.inspect
{:name => route.name, :verb => route.verb, :path => route.path, :reqs => reqs}
end
File.open(File.join(RAILS_ROOT, "routes.html"), "w") do |f|
f.puts "<html><head><title>Rails 3 Routes</title></head><body><table border=1>"
f.puts "<tr><th>Name</th><th>Verb</th><th>Path</th><th>Requirements</th></tr>"
routes.each do |r|
f.puts "<tr><td>#{r[:name]}</td><td>#{r[:verb]}</td><td>#{r[:path]}</td><td>#{r[:reqs]}</td></tr>"
end
f.puts "</table></body></html>"
end
end
精彩评论