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

Tri*_*rip 17 rake command routes ruby-on-rails

我总是被迫使我的终端窗口两个双显示器宽,只是为了看到它们正确.我不是黄油GUI的坚持者,但这是迟钝的.

这个命令有漂亮的印记吗?

Pie*_*ius 10

编辑:下面的答案被打包到支持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
Run Code Online (Sandbox Code Playgroud)


BKS*_*eon 8

使用 rails 提供的内置显示

  1. 启动服务器 rails s
  2. 单击此链接:http://localhost:3000/rails/info/routes - 这应该比在终端中看起来好多了 + 您也可以搜索路线(这非常方便)。就个人而言,我已将其保存为书签。
  3. 这是它的样子 - 点击图片放大:

这是它的样子!


Vib*_*uti 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
Run Code Online (Sandbox Code Playgroud)


Mar*_*n13 5

轨道 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
Run Code Online (Sandbox Code Playgroud)

以及PR 的链接

  • 看起来棒极了。非常感谢分享 (2认同)

Ben*_*hes 4

我稍微重写了 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
Run Code Online (Sandbox Code Playgroud)

倒数第二行仅适用于 Mac OSX 和 Rails 2.x,但它会自动在浏览器中打开文件。如果您使用不同的平台,则必须更改命令。

如果您运行的是 Rails 3.x,倒数第二行应该是

 `open #{File.join(Rails.root, "routes.html")}`
Run Code Online (Sandbox Code Playgroud)

  • 它很聪明。我绝对为此向你表示敬意,但我不喜欢一次获得所有路线。我通常会选择“耙式路线” grep 一些特定的东西` (3认同)