2
0

More than 1 year has passed since last update.

【Rails】APIモードのルーティング例

Posted at

はじめに

 本記事は、プログラミング初学者が、学習を進めていて疑問に思った点について調べた結果を備忘録も兼ねてまとめたものです。
 そのため、記事の内容に誤りが含まれている可能性があります。ご容赦ください。
 間違いを見つけた方は、お手数ですが、ご指摘いただけますと幸いです。

Rails APIモードのルーティング

ルーティング例

コメントで解説を記述していますのでご確認ください。

config/routes.rb
Rails.application.routes.draw do
  # namespaceで名前空間を付与する。
  namespace :api do
    # APIに大きな変更が発生する場合に備えてスイッチングしやすいようにURL自体にバージョンを持たせる。
    # ただし、バージョンは「必要な場合にのみ」つけることが推奨されている。
    namespace :v1 do
      # resouresはindex、show、new、edit、create、update、destroyアクションに対応するルーティングを作成できる。
      resources :restaurants do
        # only: %i[index]等と記述することで特定のアクションのみルーティングを設定することができる。
        resources :foods, only: %i[index]
      end
      resources :line_foods, only: %i[index create]
      # resourcesでは設定することのできないルーティングは以下のように記述することで設定可能
      put 'line_foods/replace', to: 'line_foods#replace'
      resources :orders, only: %i[create]
    end
  end

上記の通り記述すると以下のようなルーティングは以下のようになります。

ターミナル
 % rails routes
                                  Prefix Verb   URI Pattern                                                                                       Controller#Action
                 api_v1_restaurant_foods GET    /api/v1/restaurants/:restaurant_id/foods(.:format)                                                api/v1/foods#index
                      api_v1_restaurants GET    /api/v1/restaurants(.:format)                                                                     api/v1/restaurants#index
                                         POST   /api/v1/restaurants(.:format)                                                                     api/v1/restaurants#create
                       api_v1_restaurant GET    /api/v1/restaurants/:id(.:format)                                                                 api/v1/restaurants#show
                                         PATCH  /api/v1/restaurants/:id(.:format)                                                                 api/v1/restaurants#update
                                         PUT    /api/v1/restaurants/:id(.:format)                                                                 api/v1/restaurants#update
                                         DELETE /api/v1/restaurants/:id(.:format)                                                                 api/v1/restaurants#destroy
                       api_v1_line_foods GET    /api/v1/line_foods(.:format)                                                                      api/v1/line_foods#index
                                         POST   /api/v1/line_foods(.:format)                                                                      api/v1/line_foods#create
               api_v1_line_foods_replace PUT    /api/v1/line_foods/replace(.:format)                                                              api/v1/line_foods#replace
                           api_v1_orders POST   /api/v1/orders(.:format)                                                                          api/v1/orders#create
           
2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0