Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 725
Richlewis Avatar asked Jan 27 '13 20:01

Richlewis


2 Answers

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" %>
like image 129
James Chevalier Avatar answered Nov 16 '22 15:11

James Chevalier


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>
like image 41
Bob Avatar answered Nov 16 '22 16:11

Bob