1

So I understand in order to get the full URL on the browser you need javascript something like:

var url = window.location.href;

This by default will get the compelete URL like:

http://localhost:3000/contacts/#reference
http://localhost:3000/contacts/item

If I place the above javascript code on my application.js file, how am I going to call it on my server side like for instance on my /helpers/application_helper.rb file?

Also do I need to put my javascript into a function in order to get access with it?

Please help!

EDIT:

request.fullpath or request.orginal_url won't work on special anchor character such as http://localhost:3000/contacts/#reference that's the reason why I am asking how can I pass the js to server side.

  • 1
    Browsers do not send the hash segment of a url to the server. So if you click a link that reads `example.com/foo#bar` the server will get the reqeust as `example.com/foo`. This applies not just to Rails but to any language / framework. If you want to pass data in the URL to the server you need to place it in the query string. Can you rephrase you question so that its not a [X&Y problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem)? Focus less on the how and instead tell us what the actual problem you are trying to solve is. – max Jan 31 '20 at 13:15

2 Answers2

4

To get current URL in /helpers/application_helper.rb You need to write the rails code like:

def get_current_url
  request.original_url
end

If you're curious about more details, check the documentation original_url

If you want value from js

In application.js

function get_url {
 var url = window.location.href;
}

In application_helper.rb

def get_full_url_helper
 javascript_tag(
     "get_url()\";"
 )
end
Ibraheem Zafar
  • 346
  • 1
  • 9
  • 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. –  Jan 31 '20 at 06:58
  • `request.original_url` is server side. It will ignore the `#` or special character. I even tried this and got an undefined method error. –  Jan 31 '20 at 06:59
  • 1
    User "javascript_tag" to get function value in rails server – Ibraheem Zafar Jan 31 '20 at 07:23
0

Are you running a full-stack rails application using .erb for the views? Need some more info....because generally in rails your back end will know what URL your at? you need to go through the router to get there. unless your making some sought of api call to rails from a client server?

Roakz
  • 51
  • 6
  • If your just wanting to get your current url from the backed of a fullstack rails app check out the response here https://stackoverflow.com/questions/2165665/how-do-i-get-the-current-absolute-url-in-ruby-on-rails – Roakz Jan 31 '20 at 06:49
  • Thank you I already read this before I even posted this question and this did not help me. –  Jan 31 '20 at 07:00