Ruby on Rails how to get current URL?

2019-04-18 04:21发布

问题:

I'm trying to get the current url in rails using request.host, request.url but I have a website that are in 2 domains, so:

when I'm at www.example1.com the request.host or .url show www.example1.com

when I'm at www.example2.com the request.host or .url show example1.com because the example2 is getting all content from example1 and showing in the example2(parsed)

because the request.host get really the host, and request.url getting the host also I'm really need the get the url in the browser don't the host, someone can help?

OBs: I'm using rails 4 and ruby 2 OBs2: request.domain don't works, this return just ".com" of url

回答1:

request.original_url 

shall provide you current url in rails 4.

You can visit this resource for more info @ How do I get the current absolute URL in Ruby on Rails?



回答2:

For Rails 4 you should use request.original_url to get the current URL. More detail.