0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ルーティングを書く

Last updated at Posted at 2024-12-15

Rails.application.routes.draw do ... endブロックは消すことはできない

1.3 Railsルーターを設定する
アプリケーションやエンジンのルーティングはconfig/routes.rbファイルの中に存在します。これは、典型的なRailsアプリケーションでのルーティングの配置場所です。

次のセクションでは、このファイルで使われるさまざまなルーティングヘルパーについて説明します。

Rails.application.routes.draw do
  resources :brands, only: [:index, :show] do
    resources :products, only: [:index, :show]
  end

  resource :basket, only: [:show, :update, :destroy]

  resolve("Basket") { route_for(:basket) }
end

これは通常のRubyソースファイルなので、ルーティング定義には、条件やループなど、Rubyのあらゆる機能を利用できます。

ルーティング定義をラップするRails.application.routes.draw do ... endブロックは、ルーターDSL(Domain Specific Language: ドメイン固有言語)のスコープを確定するのに不可欠なので、削除してはいけません

  • DSLを使っているのか

resourcesによりindex、show、new、edit、create、update、destroyアクションを個別に宣言しなくても1行で宣言が完了

リソースベースのルーティング: Railsのデフォルト
リソースベースのルーティング(以下リソースルーティング)を使うことで、指定のリソースコントローラでよく使われるすべてのルーティングを手軽に宣言できます。resourcesを宣言するだけで、コントローラのindex、show、new、edit、create、update、destroyアクションを個別に宣言しなくても1行で宣言が完了します。

2.1 Web上のリソース
ブラウザはRailsに対してリクエストを送信する際に、特定のHTTP verb(GET、POST、PATCH、PUT、DELETEなど)を使って、URLに対するリクエストを作成します。上に述べたHTTP verbは、いずれもリソースに対して特定の操作の実行を指示するリクエストです。リソースルーティングでは、関連するリクエストを1つのコントローラ内のアクションに割り当てます。

Railsアプリケーションが以下のHTTPリクエストを受け取ったとします。

DELETE /photos/17
このリクエストは、特定のコントローラ内アクションにマッピングさせるようルーターに要求しています。最初にマッチしたのが以下のルーティングだとします。

resources :photos

RailsはこのリクエストをPhotosController内のdestroyアクションに割り当て、paramsハッシュに{ id: '17' }を含めます。

Rails.application.routes.draw do

  resources :books
end
                                  Prefix Verb   URI Pattern                                                                                       Controller#Action
                                   books GET    /books(.:format)                                                                                  books#index
                                         POST   /books(.:format)                                                                                  books#create
                                new_book GET    /books/new(.:format)                                                                              books#new
                               edit_book GET    /books/:id/edit(.:format)                                                                         books#edit
                                    book GET    /books/:id(.:format)                                                                              books#show
                                         PATCH  /books/:id(.:format)                                                                              books#update
                                         PUT    /books/:id(.:format)                                                                              books#update
                                         DELETE /books/:id(.:format)                                                                              books#destroy

resourcesを使うとこの表のようにこれだけのルーティングを設定できる

resources :photosとするだけでエイリアスもできる

0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?