14

Right now I have this link in my navbar. But once I moved things into a Rails app is has not worked.

<li><a href="products.html#ultra">Ultra</a></li>

I understand it needs to be changed to

<li><%= link_to "Ultra", product_path %></li>

but I want it to go directly to the Anchor I have set up on the product page. How is this done in Rails?

Corey Holmes
  • 368
  • 3
  • 13

1 Answers1

20

The url helper method can take an anchor hash as a parameter, and will output it as an anchor link:

<li><%= link_to "Ultra", product_path(anchor: 'ultra') %></li>

From the docs:

link_to can also produce links with anchors or query strings:

link_to "Comment wall", profile_path(@profile, anchor: "wall")
# => <a href="/profiles/1#wall">Comment wall</a>
p4sh4
  • 3,292
  • 1
  • 20
  • 33
  • Thanks! One more quick questions. I'm a little lost on how to change Data-Attributes as well. I am unsure how to rewrite this so that it will work. – Corey Holmes Jan 26 '16 at 03:03
  • http://stackoverflow.com/questions/9401942/using-link-to-with-embedded-html http://stackoverflow.com/questions/8734722/rails-link-to-helper-with-data-attribute – p4sh4 Jan 26 '16 at 03:06
  • Ideally, you shouldn't ask unrelated questions in comments - better make a new question, but even better to do a search first ;) – p4sh4 Jan 26 '16 at 03:07