How to link to an anchor

by Melvin Ram

If you need to link to a particular part of your website, link anchors are what you need. And linking to link anchors is really simple in Rails. Here’s an example:

<%= link_to "Google Analytics", features_path(:anchor => "analytics") %>

This will output:

<a href="/features_path#analytics">Google Analytics</a>

Here’s another example:

<%= link_to "San Francisco Offices", :controller => 'locations',
                                     :action => 'index',
                                     :anchor => 'sf' %>

As you’ve probably noticed, the key part is :anchor => ‘analytics’

Leave a Comment