Richlewis
Richlewis

Reputation: 15374

link_to tag on same page rails 3

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

Upvotes: 14

Views: 8068

Answers (2)

James Chevalier
James Chevalier

Reputation: 10874

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

Upvotes: 18

Bob
Bob

Reputation: 2121

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>

Upvotes: 15

Related Questions