Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Link to an Anchor on page in Rails App

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?

like image 730
Corey Holmes Avatar asked Jan 26 '16 02:01

Corey Holmes


1 Answers

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>
like image 105
p4sh4 Avatar answered Nov 08 '22 16:11

p4sh4