Reputation:
I was wondering if there is anyway in Rails 5 or 6 to get the full current url being visited.
For instance:
http://localhost:3000/#about
http://localhost:3000/books
http://localhost:3000/books/sale
http://localhost:3000/books/#reference
I tried request.fullpath
but it doesn't work on special characters like #
from http://localhost:3000/#about
or http://localhost:3000/books/#reference
I just need to full URL as it is. Is there anyway to do this?
Upvotes: 1
Views: 208
Reputation: 371
Browsers strip the anchor text (anything after and including the #
character) before sending the request to the server. So, this information will not by default be available to you.
If you want to read this information on the server side, you will have to include the anchor text as a parameter of the request, as well as setting the anchor variable. (setting the anchor variable exclusively modifies the href attribute of the html tag generated)
E.g.
<%= link_to "Books", books_path(params.merge(anchor: "sent-to-server")), anchor: "rendered-in-dom" %>
The anchor can then be read on the server side with
params[:anchor]
Upvotes: 0
Reputation: 18444
Url part after #
is called anchor, it is not sent to the server and is handled entirely in browser (by default it scrolls to a
element with same id
, also usually it is overloaded by SPAs to get some similar effect).
The only of getting the full page location in users' address bar is to get it from javascript window.location
and somehow send to backend.
Upvotes: 2