我总是被迫将我的终端窗口调宽两个双显示器,以查看是否正确读取它们。我不是一个黄油图形用户界面的固执者,但这太荒谬了。
这个命令有漂亮的输出吗?
发布于 2012-12-03 04:28:02
发布于 2012-01-28 15:57:11
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
发布于 2010-10-22 00:58:47
我稍微重写了rake routes命令,以生成更有用的rake routes输出的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和in rails 2.x,但它会自动在浏览器中打开文件。如果您使用的是不同的平台,则必须更改命令。
如果您运行的是Rails 3.x,倒数第二行应该是
`open #{File.join(Rails.root, "routes.html")}`
https://stackoverflow.com/questions/3986997
复制相似问题