Link to a page anchor in a Rails application

Now I have this link in my navigator. But as soon as I moved things to the Rails application, this did not work.

<li><a href="products.html#ultra">Ultra</a></li>

I understand that it needs to be changed to

<li><%= link_to "Ultra", product_path %></li>

but I want it to go directly to the Anchor that I installed on the product page . How is this done in Rails?

+4
source share
1 answer

The hello helper method can take a hash anchoras a parameter and display it as a binding:

<li><%= link_to "Ultra", product_path(anchor: 'ultra') %></li>

From the docs :

link_to can also create links with anchors or query strings:

link_to "Comment wall", profile_path(@profile, anchor: "wall")
# => <a href="/profiles/1#wall">Comment wall</a>
+4
source

Source: https://habr.com/ru/post/1625929/


All Articles