5
7

More than 5 years have passed since last update.

link_to で戻るボタン実装するときReferrer設定されてなかったらどうなるのか調べた

Posted at

Rails の ActionView::Helpers::UrlHelper#link_to についてのtipsです。
戻るボタンを実装するとき、

link_to '戻る', :back

のようにシンボルで :back と渡すとReferrerを読みに行きリンクを生成します。
しかしReferrerが設定されていない場合どうなるんだろうと思いました。

そこでドキュメントを見ると
https://api.rubyonrails.org/v5.2.0/classes/ActionView/Helpers/UrlHelper.html

Using a :back Symbol instead of an options hash will generate a link to the referrer (a JavaScript back link will be used in place of a referrer if none exists).

つまりReferrer が設定されていないときは自動的にJavaScriptのback link javascript:history.back() が呼ばれるようです。

該当コードはこの辺です。
https://github.com/rails/rails/blob/master/actionview/lib/action_view/helpers/url_helper.rb#L45-L48

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