Is there a way to make “rake routes” look better?

前端 未结 9 1309
谎友^
谎友^ 2021-02-04 04:38

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 ther

相关标签:
9条回答
  • 2021-02-04 05:23

    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
    
    0 讨论(0)
  • 2021-02-04 05:32

    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.

    0 讨论(0)
  • 2021-02-04 05:34

    For Rails 3, you can use : Rails.application.routes.routes.to_a (see my original answer)

    0 讨论(0)
提交回复
热议问题