1
2

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 5 years have passed since last update.

【Rails】絶対パス・相対パス・ルートパスの使い分け

Last updated at Posted at 2020-07-04

前提知識

  • 絶対パスとは・・・Prefixに「_url」を付けて生成されるパスで、 先頭が「http」で始まる。

  • 相対パスとは・・・現在いる階層からのパスで、先頭が「/(スラッシュ)」で始まらない。

  • ルートパス(ルート相対パスともいう)とは・・・Prefixに「_path」を付けて生成されるパスで、先頭が「/(スラッシュ)」で始まる。

使い分け

  • link_toメソッドの引数で指定するパスは、絶対パスまたはルートパスにする
application.html.erb
ex) <%= link_to("TweetApp", "/posts/index") %>

※link_toでのパスは、/の左側には「localhost:3000」がある。その為先頭の/を消すとエラーが起きる。

  • redirect_toメソッドで指定するパスは、**絶対パス(ルートパスでも可の場合が多い)**にする。
users_controller.rb
ex) redirect_to("/login")
  • renderメソッドで指定するパスは、相対パスにする。
users_controller.rb
ex) render("users/login_form")
1
2
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
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?