列出所有 rails 个关联了 http 方法的路由

List all rails routes with http method associated

我使用此方法列出我的 rails 应用程序的所有路由:

def routes
  Rails.application.routes.routes.collect do |r|
    { path: r.path.spec.to_s }
  end
end

我的结果是:

[
  { :path => '/my/path/' },
  { :path => '/my/path2/' },
  ...
]

我还想获取用于该路由的 http 方法。怎么做?
浏览 class 文档我找不到它。

预期结果是:

[
  { :path => '/my/path/', :method => :get },
  { :path => '/my/path2/', :method => :post },
  ...
]

有没有办法获取与路由关联的http方法? (或方法列表)

ActionDispatch::Journey::Route has a verb method which returns a RegExp:

你可以试试这个:

def routes
    Rails.application.routes.routes.collect do |r|
        { path: r.path.spec.to_s, :verb => r.verb.source[/[a-z]+/i].to_sym }
    end
end