Ruby on Rails では、ルーティングの設定に応じてルーティングヘルパー(ActionView::Helpers::UrlHelper)のメソッドが利用できるようになり、Railsサーバーのrails/info/routesにブラウザからアクセスすることでルーティングヘルパーメソッドの名称を確認することができます。
例
| Helper | HTTP Verb | Path | Controller#Action |
|---|---|---|---|
| admin_articles_path | GET | /admin/articles | admin/articles#index |
参考:Rails のルーティング - Railsガイド 2.3 パスとURL用ヘルパー
しかし、ここで一部ルーティングヘルパーのメソッドが表示されないパスが存在することがあります。
これは「同一のパスでHTTPメソッドが異なるルーティング」ではなく「同じパス全てでルーティングヘルパーがない」というような状況です。
このように、Railsにおいてルーティングヘルパーが作られない条件が何かしらあるように思うのですが、その条件の仕様について説明された公式に近いドキュメントはありますでしょうか?
追記
問題を確認したルーティング設定はプロダクトコードでそのまま設定を載せることが出来ないため、問題の症状が確認され続ける限り設定を削減して、最終的に残った設定のうち名称を変更したものを以下に載せます。
ruby
1# config/routes.rb 2Rails.application.routes.draw do 3 resources :hoge1, only: [] do 4 collection do 5 scope module: :bar do 6 resources :fuga 7 end 8 end 9 end 10 resources :hoge2, only: [] do 11 collection do 12 resources :fuga 13 end 14 end 15end
このとき、hoge2/fuga に関するパスでルーティングヘルパーが作られない現象を確認できました。
現象が起こるのはresources :hoge1とresources: hoge2のそれぞれの中にあるresources :fugaの:fugaの名称がそれぞれで一致している場合に現象を再現できました。
追記
上記のconfig/routes.rbの設定の時、Railsサーバーのrails/info/routesにアクセスして得られるルーティングの情報は以下のようになります。
| Helper | HTTP Verb | Path | Controller#Action |
|---|---|---|---|
| fuga_index_path | GET | /hoge1/fuga(.:format) | bar/fuga#index |
| POST | /hoge1/fuga(.:format) | bar/fuga#create | |
| fuga_path | GET | /hoge1/fuga/:id(.:format) | bar/fuga#show |
| PATCH | /hoge1/fuga/:id(.:format) | bar/fuga#update | |
| PUT | /hoge1/fuga/:id(.:format) | bar/fuga#update | |
| DELETE | /hoge1/fuga/:id(.:format) | bar/fuga#destroy | |
| GET | /hoge2/fuga(.:format) | fuga#index | |
| POST | /hoge2/fuga(.:format) | fuga#create | |
| GET | /hoge2/fuga/:id(.:format) | fuga#show | |
| PATCH | /hoge2/fuga/:id(.:format) | fuga#update | |
| PUT | /hoge2/fuga/:id(.:format) | fuga#update | |
| DELETE | /hoge2/fuga/:id(.:format) | fuga#destroy |
ご覧のように /hoge1/fuga(.:format)と/hoge1/fuga/:id(.:format)についてはルーティングヘルパーが示されていますが、/hoge2/fuga(.:format)と/hoge2/fuga/:id(.:format)のルーティングヘルパーは示されません。