'Rails link_to with same page anchor and class name
I'm trying to use rails link_to to produce
<a href="/#about" class="leftnav__link">about</a>
I have tried
<%= link_to "about", anchor: "about", class: "leftnav__link" %>
which results in
<a href="/?class=leftnav__link#about">about</a>
The anchor surprisingly works but the class name is not placed correctly. What am I missing?
Solution 1:[1]
Your example is mixing the options and html_options arguments (docs).
If you're trying to link to the an about element on the page at /, you'd need something like this:
<%= link_to "about", root_path(anchor: 'about'), class: "leftnav__link" %>
If you're trying to link to the about element on the current page (just to scroll to it), you'd do something like:
<%= link_to "about", '#about', class: "leftnav__link" %>
or
<%= link_to "about", { anchor: 'about' }, class: "leftnav__link" %>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Mike |
