I am trying to link to an element on the same page using link_to
in basic HTML i would do this
<a href="#tom">Tom Roche</a>
<a id="tom">Chapter 4</a>
I have this in my app
<%= link_to 'Tom Roche' %>
<h2>Tom Roche</h2>
How would i link these so that when i click on Tom Roche i get taken to the h2 with Tom Roche
I have tried this
<%= link_to 'Tom Roche', our_team_path(:anchor => '#tom') %>
<h2><a id="tom">Tom Roche</a></h2>
but its not working
Can anyone point out what i need to do, the link_to is throwing me for some reason, even though i know its an a tag
Thanks
If you want to link to an anchor tag on the same page the visitor is currently viewing, you can shorten the link_to
code a bit:
<%= link_to "Comment wall", anchor: "wall" %>
link_to can also produce links with anchors or query strings:
http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html
link_to "Comment wall", profile_path(@profile, :anchor => "wall")
# => <a href="/profiles/1#wall">Comment wall</a>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With