【Ruby on Rails】使用するアクションのルーティングだけを設定する方法
リソースベースのルーティングを使ってroutes.rbファイルに設定を記述すると自動で7つのルーティングが設定されてしまい、邪魔です。
post_comments GET /posts/:post_id/comments(.:format) comments#index POST /posts/:post_id/comments(.:format) comments#create new_post_comment GET /posts/:post_id/comments/new(.:format) comments#new edit_post_comment GET /posts/:post_id/comments/:id/edit(.:format) comments#edit post_comment GET /posts/:post_id/comments/:id(.:format) comments#show PATCH /posts/:post_id/comments/:id(.:format) comments#update PUT /posts/:post_id/comments/:id(.:format) comments#update DELETE /posts/:post_id/comments/:id(.:format) comments#destroy
例えば上記のうち、createとdestroyのみルーティング設定したい場合、
以下のようにroutes.rbに設定すればOK
既存の設定
resources :posts do resources :comments end
必要なもののみ設定
resources :posts do resources :comments, only: [:create, :destroy] end