Post

railsのrouteでidを必要としないルーティングを追加したい

解決

collection を使用する。

比較

resources :notifications, only: %i[index] do get "/on_top", to: "notifications#on_top" # notification_under GET /notifications/:notification_id/under(.:format) notification#under collection do get "/on_top", to: "notifications#on_top" # under_notifications GET /notifications/under(.:format) notification#under end member do get "/on_top", to: "notifications#on_top" # under_notification GET /notifications/:id/under(.:format) notification#under end end

Rails 5 Routing Cookbook: 10 recipes for the novice Rails developer and beyond

事象

idを必要としないルート(/hoge/new_route)を追加しようとしたがid が必要のルート(/notifications/:notification_id/under)になる。

resources :notifications, only: %i[index] do get "/on_top", to: "notification#on_top" end
This post is licensed under CC BY 4.0 by the author.