Rails: Adding an Anchor to a link_to URL – Mr. Joel Kemp

I wanted to add a specific anchor in a link_to()’s URL in rails. This should have been a simple task, but it was quite a headache since the hash kept getting escaped into the “%23” code.

I tried unescaping the html string using HAML’s filters, to no avail. I also tried various forms of interpolation but no luck.

The solution was to use the :anchor attribute/symbol of the link_to view helper and set the attribute’s value to the ID of the desired anchor.

Lesson learned: if numerous Google searches fail you, check the official documentation.