我总是被迫将我的终端窗口设置为两个双显示器宽,只是为了正确地阅读它们。我不是黄油 GUI 的坚持者,但这太荒谬了。
这个命令有漂亮的打印吗?
编辑:下面的答案已打包到支持 Rails 3 和 4 的 html_routes gem 中。
下面的代码是用当前的 Rails 3.2.3 编写的,按控制器分组,看起来棒极了。 请记住将
<Your APP>
更改为您的应用程序名称,并将 gem“语法”添加到您的 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
rails s
对于那些无论如何都想使用终端来查看复杂内容的人
rails routes
。
Rails 6 引入了
--expanded
选项以更方便的方式打印它们。
这是一个例子:
$ 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
还有一个 公关链接。
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
我稍微重写了 rake 路由命令,以生成稍微更可用的 html 版本的 rake 路由输出
创建一个文件
pretty_routes.rake
并将其放入lib/tasks/
并调用rake pretty_routes
,它应该会稍微好一点
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
倒数第二行仅适用于 Mac OSX 和 Rails 2.x,但它会自动在浏览器中打开文件。 如果您在不同的平台上,则必须更改命令。
如果您运行的是 Rails 3.x,倒数第二行应该是
`open #{File.join(Rails.root, "routes.html")}`
您可以使用 Sextant 在浏览器中打印路线:https://github.com/schneems/sextant
很棒的提示。谢谢。
我准备了Rails 3.0的工作版本。享受吧。
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