1
3

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.

link_toで指定したIDのページへ飛ばす

Last updated at Posted at 2018-12-13

少しハマったので忘備録として残します。

  1. 飛ばしたい位置(指定したいリンク先の場所)のタグにidを指定
home/index.html.erb
<h1 class="display-4" id="service">サービス概要</h1>
省略

  1. pathの引数にanchor: "ID名"を渡してあげる
layouts/_header.html.erb
<%= link_to "サービス概要", root_path(anchor: "service"), class: "nav-item nav-link" %>

同時にclassも指定することも可能です。


以上です。簡単ですね。

上の例の場合だと、ヘッダーのナビリンク内のにサービス概要というリンクがあり、どのページからでもサービス概要をクリックすれば、home/indexページ内のサービス概要と<h1>で書かれている場所まで飛ばしてくれます。

1
3
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
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?