Home > Mobile >  Rails link_to with same page anchor and class name
Rails link_to with same page anchor and class name

Time:03-31

I'm trying to use rails link_to to produce

<a href="/#about" >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?

CodePudding user response:

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" %>
  • Related