RAILS 5: Appending an HTML anchor to rails route path helper
Asked Answered
H

2

11

I have a button that directs the user to another page:

<%= link_to "Jacuzzis", applications_path, class: "dropdown-item"%>

At the moment, this will lead the user to my 'applications' page, but there are many applications on this page. So I want to append the 'jacuzzis' id to the path variable so that the browser jumps to that section upon page load.

E.g instead of a GET request to: /applications, it's /applications#jacuzzis

I guess something like <%= link_to "Jacuzzis", (applications_path + '#jacuzzis'), class: "dropdown-item"%>

Hinson answered 17/8, 2018 at 12:21 Comment(0)
C
12

You can pass the :anchor option to your path helper.

<%= link_to "Jacuzzis", applications_path(anchor: 'jacuzzis'), class: "dropdown-item" %>

Reference: https://github.com/rails/rails/blob/85b533308f5ddcb9a59853bce38a113b66b13faa/actionview/lib/action_view/helpers/url_helper.rb#L172-L175

Crenation answered 17/8, 2018 at 12:29 Comment(0)
A
1

You can use this in rails way

<%= link_to post_path(@comment.post, anchor: 'some-id'), class: "dropdown-item" %>

Hope this will help

Thanks

Astarte answered 17/8, 2018 at 12:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.