1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

ルーティングを理解する

Posted at

ルーティングの役割

ルーティングはコントローラーを繋ぐ役割を持っていて、以下の順番で処理を行う。

①URLを検索(リクエスト)
②ルーティング
③コントローラー
④ビュー
⑤レスポンスを返す

ルーティング理解する

公式
get "URL" => "コントローラー名#アクション名"
実践
ブラウザから、「localhost:3000/home/top」というURLが送信された時

Rails.application.routes.draw do
  get "home/top" => "home#top"
end

上記のように記述すると、homeコントローラーのtopアクションで処理されるようになる。

⚠︎ "home/top"はURLを表している
⚠︎ "home#top"は"コントローラー名#アクション名"

もし、上記のようなルーティングの記述がしてあるのに、「localhost:3000/home/hello」を送信すると、
ルーティングエラーが起きる。これは、URL(home/hello)に対応するルーティングが存在しないためである。
1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?