LoginSignup
52
37

More than 5 years have passed since last update.

_pathと_urlの違いについて調べてみた

Posted at

そもそも_pathと_urlってなんだ?

ヘルパーの一種。ルーティングを作成すると、アプリケーションのコントローラで多くのヘルパーが利用できるようになりますがその内の一つみたいです。

相対パスと絶対pathの違いってなんだ?

そもそもそこからでした。相対パスは今いる位置を起点にして、場所を指定する。絶対パスは今いる位置に関係なく場所を指定する。

root_path => '/'
root_url  => 'http://www.example.com/'

new_path => '/new'
new_url => 'http://www.example.com/new'

_path

・相対パス
・redirect_to以外で使用する。
・link_toでよく使用されるイメージ

_url

・絶対パス
・redirect_toの時にセットで使用する。(HTTPの仕様上、リダイレクトのときに完全なURLが求められるので)

参考記事

Rails のルーティング
pathメソッドと_urlメソッドの使い分け

52
37
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
52
37