有没有办法让“耙路”看起来更好?

时间:2010-10-21 11:39:15

标签: ruby-on-rails routes rake command

我总是被迫使我的终端窗口两个双显示器宽,只是为了看到它们正确。我不是黄油用户界面的坚持者,但这种情况有点迟钝。

这个命令有漂亮的印刷品吗?

9 个答案:

答案 0 :(得分:10)

编辑:以下答案被打包到支持Rails 3和4的html_routes gem中。

以下代码是使用当前的 Rails 3.2.3 ,按控制器进行分组,看起来很棒。请记住将<Your APP>更改为您的应用名称,并将gem'语法'添加到您的Gemfile中。

Sample image

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

答案 1 :(得分:9)

我认为

https://github.com/nicooga/color_routes做得很好!

答案 2 :(得分:5)

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

答案 3 :(得分:4)

我稍微重写了rake routes命令,以生成稍微更有用的rake路由输出的html版本

创建一个文件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")}`

答案 4 :(得分:3)

您可以使用Sextant在浏览器中打印路线:https://github.com/schneems/sextant

答案 5 :(得分:2)

对于Rails 3,您可以使用:Rails.application.routes.routes.to_a(请参阅my original answer

答案 6 :(得分:1)

很棒的提示。感谢。

我为 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

答案 7 :(得分:1)

浏览器中的

http://localhost:3000/rails/info/routes看起来比终端更好。

答案 8 :(得分:1)

路轨6 +

对于那些仍然想使用其终端查看复杂的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

还有一个link to PR