LoginSignup
9
5

More than 5 years have passed since last update.

link_toでページ内リンクとclass指定を共存させる方法

Posted at

やろうとして、ちょっと詰まったので忘れないうちにメモ。

<a class="button" href="#about">LEARN MORE</a>

こんな感じのHTMLをlink_toで生成したいとする。

<%= link_to("LEARN MORE", anchor: "about", class: "button") %>

これだと駄目。

<%= link_to("LEARN MORE", { anchor: "about" }, class: "button") %>

こうやって、明示的に二番目のパラメータをHashにすることで、うまくいった。

こうすることで、class: "button" をHTMLパラメータ(三つ目のパラメータ)として認識させることができるんだと思う。

と考えると、上記であげた駄目な例は、以下の書き方と同義かな?

<%= link_to("LEARN MORE", { anchor: "about", class: "button" } ) %>
9
5
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
9
5