0

rake ルートの配列があり、route.conditions[:request_method] を GET として持つすべてのルートを抽出しようとしています。

問題:

:request_method is a regex (:request_method=>/^GET$/)

> routes.select { |route| route.conditions[:request_method] == /GET/ }  
> []

私の選択は正しいと思いました。これは機能し、すべてのルート メソッドを出力します。

> routes.each { |route| print route.conditions[:request_method] }
> {:request_method=>/^GET$/}{:request_method=>/^GET$/}{:request_method=>/^PUT$/}{:request_method=>/^GET$/}{:request_method=>/^PUT$/}{:request_method=>/^POST$/}{:request_method=>/^GET$/}{:request_method=>/^GET$/}

これを達成する方法はありますか?

4

1 に答える 1

0

私の答えを見つけました。routes.each は Journey オブジェクトを返していたので、最初に独自のカスタム ハッシュを作成しました。

routes = Rails.application.routes.routes.to_a
routes = routes.collect { |route| {name: route.name, method: route.verb} }
routes = routes.select { |route| route[:method] == /^GET$/ }

これはもっとエレガントに作り直すことができると確信しているので、変更を受け入れます。それは機能します、それが最も重要な部分です。

于 2012-08-03T17:40:10.340 に答える